Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 790
Garima Srivastava Avatar asked Dec 09 '22 06:12

Garima Srivastava


2 Answers

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;
like image 132
alex Avatar answered Dec 10 '22 19:12

alex


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";
like image 39
Haitao Li Avatar answered Dec 10 '22 20:12

Haitao Li