Search code examples
iosobjective-cphotosframework

iOS Photos Framework data UTI or format when image is in cloud


I'm upgrading my GIF app to use the iOS 8 Photos Framework and basically I want to find all the user's GIFs in their photo library and display them. Worked great with ALAssetLibrary before iCloud Photo Library's 'optimise storage' came into play as now users can have a large amount of their images in the cloud, hence needing to use the Photos Framework to get at them Unfortunately it seems that you can't tell if a PHAsset is GIF or a normal static image until you've downloaded it from the cloud.... Which is going to make my app pretty lame when you have zero or poor network connection...

This is the code I use to attempt to get only the GIFs:

PHFetchOptions* fetchOptions = [[PHFetchOptions alloc] init];
            fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
            PHFetchResult *allPhotosResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options: fetchOptions];

            //   Get assets from the PHFetchResult object
            [allPhotosResult enumerateObjectsUsingBlock:^(PHAsset *asset, NSUInteger idx, BOOL *stop) {
                //NSLog(@"asset %@", asset);
                PHImageRequestOptions* options = [[PHImageRequestOptions alloc] init];
                options.synchronous = YES;
                options.deliveryMode = PHImageRequestOptionsDeliveryModeFastFormat;
[imageManager requestImageDataForAsset: asset options: options resultHandler:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) {

// when the item is in the cloud dataUTI is nil so I can't check what format it is...

}

The info dictionary contains some undocumented keys:

  • PHImageResultDeliveredImageFormatKey
  • PHImageResultWantedImageFormat

Which I've seen have values of 9999, 9998 and 40xx. But they don't particularly seem to correlate with whether the image is a GIF or not so must relate to something else entirely...

Any ideas??


Solution

  • Looks like there just isn't a way to figure this out without pulling down the images from the cloud and checking after. I'm updating my app to do that but to remember the type of each image against the PHAsset's localIdentifier so that it only needs to do it once.

    If you take this approach you might also want to avoid PHAssets which represent burst, HDR or panorama images as they're not going to be gifs (obviously exclude videos too!)