这里我手动将一个名为image1.png的图像加载到iPad照片库中,并且需要在我的iPad应用程序中获取相同的图像。我的图像被替换为具有相同名称的新图像,我的意图是在不做任何代码更改的情况下随时更改图像。我没有选择将图像保存在服务器并从那里访问,所以我正在寻找这样的解决方案。请帮我是否可以通过使用图片名称从iPad照片库中获取图像
1
A
回答
0
我没有得到解决方案,使用图像名称从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];
}
}
}
}
使用此函数我解决了我的问题。它可能是有用的。
1
你可能想将其保存到本地存储:
// 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];
代码从here。
相关问题
- 1. 我可以从iPhone中的照片获取图像的名称
- 2. 从照片库使用照片框架获取所有图像
- 3. 从照片库中获取图像名称
- 4. 是否可以通过使用Javascript的图片url获取图片大小
- 5. iPad模拟器iOS6,没有从UIImagePickerController照片库获取图像
- 6. IOS图片库获取给定照片的相册名称
- 7. 获取图片名称的iPhone照片库
- 8. 如何获取可绘制图片的照片名称?
- 9. 从照片库中获取照片
- 10. 从照片库中获取图片并通过名称和路径将其上传到网络服务
- 11. 尝试通过图像选择器从照片库获取照片时,应用程序崩溃
- 12. 是否有可能从图片类获取图片令牌?
- 13. 是否可以检查图像是由相机拍摄还是通过JavaScript从照片库上传?
- 14. 从照片库中加载图像
- 15. 从Android照片库中获取所有图片
- 16. iphone sdk如何从照片库中获取完整的图像?
- 17. 如何从照片库中获取图像?
- 18. 用图api获取照片
- 19. [iOS]从照片库中选择图片
- 20. startActivityfor从片段结果 - 从图片库中获取图片
- 21. 使用xctest从照片/图库中挑选图像
- 22. 从图库中提取照片android
- 23. 无法从图库中选取照片?
- 24. 可通过API获取照片位置?
- 25. 使用图形API从带标记的照片中获取相册的名称
- 26. 使用UIImagepickercontroller从iPhone照片库获取照片
- 27. 从iphone照片库中获取照片并在表格视图中显示
- 28. FB.ui的属性'图片'可以从HttpHandler中获取图像吗?
- 29. 从图片名称
- 30. 从wordpress图片库获取一张照片
您可以通过谓词浏览PHFetchOptions()。我不确定他们是否提供图片标题查询。但是我们可以使用PHFetchOptions()来查询“creationTime”,“mediatype”等。请通过以下浏览https://developer.apple.com/library/prerelease/ios/documentation/Photos/Reference/PHFetchOptions_Class/index.html –