How to retrieve photo extension (jpg/png) in iOS 8.0 using Photos API?

百般思念 提交于 2019-12-06 04:27:56

问题


Am trying to get file extension of photos using the new Photos API in iOS 8 but haven't found a way to do so until now. Before iOS 8.0 I would use ALAssetRepresentation to get the file extension like:

// Get asset representation from asset
ALAssetRepresentation *assetRepresentation = [asset defaultRepresentation];

// Extract file extension from the original file name
NSString* fileExt = [[assetRepresentation filename] pathExtension];

Is there any way to get file extension of photos now? PS: I am using new Photos API as I need to access all photos in my photos app, and ALassetsLibrary gives access to "Recently Added" photos only.


回答1:


I've got it.

[[PHImageManager defaultManager] requestImageDataForAsset:asset options:imageRequestOptions resultHandler:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) {

        NSLog(@"info - %@", info);
    }];

PHImageFileDataKey = <PLXPCShMemData: 0x179ab6d0> bufferLength=1384448 dataLength=1384448;
PHImageFileOrientationKey = 1;
PHImageFileSandboxExtensionTokenKey = "c05e608acaf0bb212086ed2d512ccc97ea720ac3;00000000;00000000;0000001a;com.apple.app-sandbox.read;00000001;01000003;0000000000030b8c;/private/var/mobile/Media/DCIM/102APPLE/IMG_2607.JPG";
PHImageFileURLKey = "file:///var/mobile/Media/DCIM/102APPLE/IMG_2607.JPG";
PHImageFileUTIKey = "public.jpeg";
PHImageResultDeliveredImageFormatKey = 9999;
PHImageResultIsDegradedKey = 0;
PHImageResultIsInCloudKey = 0;
PHImageResultIsPlaceholderKey = 0;
PHImageResultWantedImageFormatKey = 9999;



回答2:


Here is one way to do it:

PHImageRequestOptions * imageRequestOptions = [[PHImageRequestOptions alloc] init];
imageRequestOptions.synchronous = YES;

[[PHImageManager defaultManager]
 requestImageForAsset:asset
 targetSize:CGSizeMake(2048, 2048)
 contentMode:PHImageContentModeAspectFit
 options:imageRequestOptions
 resultHandler:^(UIImage *result, NSDictionary *info) {
 }];

The file name is contained in info. For example:

PHImageFileURLKey = "file:///var/mobile/Media/DCIM/100APPLE/IMG_0066.JPG";



回答3:


**imageManager.requestImageDataForAsset((images[indexPath.row] as PHAsset), options:PHImageRequestOptions(), resultHandler: {imagedata,dataUTI,Orientation,info in

var str:String=((((info as Dictionary)["PHImageFileURLKey"])! as NSURL).lastPathComponent as String) cell.imageName.text=str as String**

|improve this answer

来源:https://stackoverflow.com/questions/25919512/how-to-retrieve-photo-extension-jpg-png-in-ios-8-0-using-photos-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!