iOS14-PHAsset获取图片路径

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获取图片路径)