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.
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;
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";
**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**
来源:https://stackoverflow.com/questions/25919512/how-to-retrieve-photo-extension-jpg-png-in-ios-8-0-using-photos-api