PHAsset获取图片的三种方式,通过PHImageManager,用下面三个实例办法去获取:
第一种:
PHImageRequestOptions *option = [[PHImageRequestOptions alloc] init];
需要 特别注意的为增加网络请求,保证可以下载从iCloud 同步过来的图片
option.networkAccessAllowed = YES;
使用PHImageManager或者它的子类PHCachingImageManager
- (PHImageRequestID)requestImageForAsset:(PHAsset *)asset targetSize:(CGSize)targetSize contentMode:(PHImageContentMode)contentMode options:(nullable PHImageRequestOptions *)options resultHandler:(void (^)(UIImage *_Nullable result, NSDictionary *_Nullable info))resultHandler;
/**
@abstract Request largest represented image as data bytes for the specified asset.
@param asset The asset whose image data is to be loaded.
@param options Options specifying how Photos should handle the request, format the requested image, and notify your app of progress or errors.
If PHImageRequestOptionsVersionCurrent is requested and the asset has adjustments then the largest rendered image data is returned. In all other cases then the original image data is returned.
@param resultHandler A block that is called exactly once either synchronously on the current thread or asynchronously on the main thread depending on the synchronous option specified in the PHImageRequestOptions options parameter (deliveryMode is ignored).
*/
第二中和第三中为同一种方法,只不过是iOS13之前和iOS8-iOS13的方法,注意使用的时候注意
- (PHImageRequestID)requestImageDataForAsset:(PHAsset *)asset options:(nullable PHImageRequestOptions *)options resultHandler:(void (^)(NSData *_Nullable imageData, NSString *_Nullable dataUTI, UIImageOrientation orientation, NSDictionary *_Nullable info))resultHandler API_DEPRECATED_WITH_REPLACEMENT("-requestImageDataAndOrientationForAsset:options:resultHandler:", ios(8, 13), tvos(8, 13)) API_UNAVAILABLE(macos);
/**
@abstract Request largest represented image as data bytes and EXIF orientation for the specified asset.
@param asset The asset whose image data is to be loaded.
@param options Options specifying how Photos should handle the request, format the requested image, and notify your app of progress or errors.
If PHImageRequestOptionsVersionCurrent is requested and the asset has adjustments then the largest rendered image data is returned. In all other cases then the original image data is returned.
@param resultHandler A block that is called exactly once either synchronously on the current thread or asynchronously on the main thread depending on the synchronous option specified in the PHImageRequestOptions options parameter (deliveryMode is ignored). Orientation is an EXIF orientation as an CGImagePropertyOrientation. For iOS or tvOS, convert this to an UIImageOrientation.
*/
- (PHImageRequestID)requestImageDataAndOrientationForAsset:(PHAsset *)asset options:(nullable PHImageRequestOptions *)options resultHandler:(void (^)(NSData *_Nullable imageData, NSString *_Nullable dataUTI, CGImagePropertyOrientation orientation, NSDictionary *_Nullable info))resultHandler API_AVAILABLE(macos(10.15), ios(13), tvos(13));
在resultHandler中拿info的@"PHImageFileURLKey",这个key值存放为图片的路径。
需注意:
如果出现异常info[@"PHImageFileURLKey"]无法获取图片路径地址,可以采用以下办法:
(1)通过PHAssetResource获取
NSArray *resources = [PHAssetResource assetResourcesForAsset:model.asset];
NSString *path = [(PHAssetResource*)resources[0] valueForKey:@"privateFileURL"];
NSURL *pathURL = (NSURL *)path;
//还可以获取文件的名称
NSString *imageFilename = ((PHAssetResource*)resources[0]).originalFilename;
(2)直接拼接
//相册路径前缀为:file:///var/mobile/Media
PHAsset *asset;
NSString *filename = [asset valueForKey:@"filename"];
NSString *directory = [asset valueForKey:@"directory"];
获取绝对路径和文件名,进行拼接即可
NSString *path = file:///var/mobile/Media/directory/filename
NSURL *pathURL = (NSURL *)path;
希望可以帮助到大家,也是一个学习的记录,如果错误,烦请指正
iOS14-PHAsset获取图片路径
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 本文介绍如何从相册,摄像头中获取图片Bitmap和图片的路径Path.并且实现可以剪裁图片,和压缩图片. 1:打开...
- 未经本人授权,不得转载!否则必将维权到底 有个需求,可以从系统相册选择图片,上传到服务器。那么选择从系统相册选择完...
- 目录 一、数据探索和预处理 二、无监督学习-降维和聚类分析 三、聚类效果对比分析 四、小结和建议 备注分析软件:p...