[英]Is it possible to get an image from iPad Photo Library by using the image name
Here I am loading one image named image1.png into iPad Photo library manually and need to get same image in my iPad app. 在这里,我将一张名为image1.png的图像手动加载到iPad照片库中,需要在我的iPad应用程序中获取相同的图像。 Image my get replaced with new image with same name, My intention is to change the image whenever needed without doing any code change. 我的图像被替换为相同名称的新图像。我的意图是在需要时更改图像,而无需进行任何代码更改。 I don't have option to save the images in server and access from there so I am looking for solution like this. 我没有选择将图像保存在服务器中并从那里访问的选项,因此我正在寻找这样的解决方案。 Please help me 请帮我
You might want to save it to the local storage: 您可能需要将其保存到本地存储中:
// Assuming 'newImage' is a 'UIImage' object containing your image
NSData *imageData = UIImagePNGRepresentation(newImage);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *imagePath =[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png",@"yourImageName"]];
NSLog((@"pre writing to file"));
if (![imageData writeToFile:imagePath atomically:NO])
{
NSLog((@"Failed to cache image data to disk"));
}
else
{
NSLog((@"the cachedImagedPath is %@",imagePath));
// Persist path in a dictionary or NSUserDefaults
}
And retrieve it later like this: 然后像这样检索它:
NSString *theImagePath = [yourDictionary objectForKey:@"cachedImagePath"];
UIImage *customImage = [UIImage imageWithContentsOfFile:theImagePath];
I didn't got solution to load image from iPad library using image name, so i saved all my images in one user library and loaded that images from my iPad app. 我没有使用图像名称从iPad库加载图像的解决方案,因此我将所有图像保存在一个用户库中并从iPad应用程序加载了这些图像。
-(void)loadImageFromLibrary
{
__block UIImage *ima;
NSMutableArray *images=[NSMutableArray new];
PHAsset *asset = nil;
PHAssetCollection *collection;
NSArray *collectionsFetchResults;
NSMutableArray *localizedTitles = [[NSMutableArray alloc] init];
PHFetchResult *userCollections = [PHCollectionList fetchTopLevelUserCollectionsWithOptions:nil];
collectionsFetchResults = @[userCollections];//to get images from user created library.
for (int i = 0; i < collectionsFetchResults.count; i ++) {
PHFetchResult *fetchResult = collectionsFetchResults[i];
for (int x = 0; x < fetchResult.count; x ++) {
collection = fetchResult[x];
localizedTitles[x] = collection.localizedTitle;//get library names
}
}//MyImages is my image library
if ([collection.localizedTitle isEqualToString:@"MyImages"]) {
PHFetchResult *fetchResult = [PHAsset fetchAssetsInAssetCollection:collection options:nil];
if (fetchResult != nil && fetchResult.count > 0) {
for (asset in fetchResult) {
PHImageRequestOptions * imageRequestOptions = [[PHImageRequestOptions alloc] init];
imageRequestOptions.synchronous = YES;
[[PHImageManager defaultManager] requestImageForAsset:asset
targetSize:PHImageManagerMaximumSize
contentMode:PHImageContentModeDefault
options:imageRequestOptions
resultHandler:^void(UIImage *image, NSDictionary *info) {
NSLog(@"info = %@", info);
ima = image;
}];
[images addObject:ima];
}
}
}
}
Using this function i solved my problem.It may be useful. 使用此功能,我解决了我的问题。它可能很有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.