【问题标题】:Get UIImage from PHAsset (or assets-library://)从 PHAsset(或 assets-library://)获取 UIImage
【发布时间】:2020-04-04 07:04:56
【问题描述】:

我正在尝试从 PHAsset 或资产库生成 UIImage。我的代码是这样的:

NSString *path = [self.options valueForKey:@"path"];
NSURL *localurl = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:localurl];
UIImage *image = [[UIImage alloc] initWithData:data];

但是 UIImage 是零。我可以将路径设置为ph://assets-library://

谢谢!

【问题讨论】:

  • @matt 阅读问题。

标签: objective-c uiimage


【解决方案1】:
PHFetchOptions *allPhotosOptions = [PHFetchOptions new];

allPhotosOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];

PHFetchResult *allPhotosResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:allPhotosOptions];

NSMutableArray *arrPhassets=[[NSMutableArray alloc]init];
[allPhotosResult enumerateObjectsUsingBlock:^(PHAsset *asset, NSUInteger idx, BOOL *stop) {
//here you will get phassets of images from the device photo library.you have to store it in the mutable array like this.
    [arrPhassets addObject:asset];
}];

//here you will have array of phassets for images.

//now we will extract image from the phasset for one phasset.
PHAsset *as1=arrPhassets[0];
PHCachingImageManager *imagemanager=[[PHCachingImageManager alloc]init];
[imagemanager requestImageForAsset:as1 targetSize:PHImageManagerMaximumSize contentMode:PHImageContentModeAspectFit options:nil resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
    //here "result" is the image for asset as1.
    UIImage *image=result;

}];

【讨论】:

  • 如何从 phsset url 获取 PHAsset,例如 ph://...?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-04
  • 2015-08-28
  • 2016-08-30
  • 2019-02-13
  • 1970-01-01
  • 2011-01-23
相关资源
最近更新 更多