Ios 是否可以使用图像名称从iPad照片库中获取图像
在这里,我手动将一张名为image1.png的图片加载到iPad照片库中,需要在我的iPad应用程序中获得相同的图片。我的图像被替换为同名的新图像,我的意图是在需要时更改图像,而不做任何代码更改。我没有选择保存在服务器上的图像和访问,所以我正在寻找这样的解决方案。请帮助我您可能希望将其保存到本地存储:Ios 是否可以使用图像名称从iPad照片库中获取图像,ios,objective-c,image,ipad,Ios,Objective C,Image,Ipad,在这里,我手动将一张名为image1.png的图片加载到iPad照片库中,需要在我的iPad应用程序中获得相同的图片。我的图像被替换为同名的新图像,我的意图是在需要时更改图像,而不做任何代码更改。我没有选择保存在服务器上的图像和访问,所以我正在寻找这样的解决方案。请帮助我您可能希望将其保存到本地存储: // Assuming 'newImage' is a 'UIImage' object containing your image NSData *imageData = UIImagePNGR
// 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
}
然后像这样检索它:
NSString *theImagePath = [yourDictionary objectForKey:@"cachedImagePath"];
UIImage *customImage = [UIImage imageWithContentsOfFile:theImagePath];
源代码。我没有使用图像名称从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];
}
}
}
}
-(void)loadImageFromLibrary
{
__块UIImage*ima;
NSMUTABLEARRY*图像=[NSMUTABLEARRY new];
相位集*资产=零;
阶段集合*集合;
NSArray*collectionsFetchResults;
NSMutableArray*localizedTitles=[[NSMutableArray alloc]init];
PHFetchResult*userCollections=[PHCollectionList fetchTopLevelUserCollectionsWithOptions:nil];
collectionsFetchResults=@[userCollections];//从用户创建的库中获取图像。
对于(int i=0;i0){
for(fetchResult中的资产){
PHImageRequestOptions*imageRequestOptions=[[PHImageRequestOptions alloc]init];
imageRequestOptions.synchronous=是;
[[PHImageManager defaultManager]requestImageForAsset:asset
targetSize:PHImageManagerMaximumSize
contentMode:PHImageContentModeDefault
选项:imageRequestOptions
resultHandler:^void(UIImage*图像,NSDictionary*信息){
NSLog(@“info=%@”,info);
ima=图像;
}];
[图像添加对象:ima];
}
}
}
}
使用此函数我解决了我的问题。它可能很有用。您可以使用谓词遍历PHFetchOptions()。我不确定他们是否提供关于图片标题的查询。但是我们可以使用PHFetchOptions()查询“creationTime”、“mediatype”等。请仔细阅读下面的内容。我不想使用应用程序保存图像,我想在用户需要时手动将图像加载到库中,用户也可以替换图像,并且图像名称应与“image1.png”相同。@Rajith asfaik这是不可能的,因为您无法设置图像名称。你总是可以得到最好的结果。您还可以启用与iTunes的文件共享,这样用户就可以在iTunes中查看和交换文件。