Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly detect a PHAsset's file type (GIF)

I have no idea why this is so difficult. I'm trying to determine the file type of a PHAsset, specifically, I want to know if a given asset represents a GIF image or not.

Simply inspecting the asset's filename tells me it's an MP4:

[asset valueForKey:@"filename"] ==> "IMG_XXXX.MP4"

Does iOS convert GIF's to videos when saved to the devices image library? I've also tried fetching the image's data and looking at it's dataUTI, but it just returns nil for GIF's (I'm assuming all videos as well). I'm fetching the image data as follows:

PHImageManager *manager = asset.imageManager ? asset.imageManager : [PHImageManager defaultManager];

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    PHImageRequestOptions *o = [[PHImageRequestOptions alloc] init];
    o.networkAccessAllowed = YES;

    [manager requestImageDataForAsset:asset.asset options:o resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {

        dispatch_async(dispatch_get_main_queue(), ^{
            CIImage *ciImage = [CIImage imageWithData:imageData];
            if(completion) completion(imageData, dataUTI, orientation, info, ciImage.properties);
        });

    }];

});

the dataUTI returned from the above call is nil.

If anyone knows of a reliable way to determine a PHAsset's file type (I'm specifically looking for GIF's, but being able to determine for any type of file would be great) let me know!

like image 703
mitchtreece Avatar asked Feb 12 '16 01:02

mitchtreece


1 Answers

Use PHAssetResource.

    NSArray *resourceList = [PHAssetResource assetResourcesForAsset:asset];
    [resourceList enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        PHAssetResource *resource = obj;
        if ([resource.uniformTypeIdentifier isEqualToString:@"com.compuserve.gif"]) {
           isGIFImage = YES;
        }
    }];
like image 127
dongkang Avatar answered Sep 23 '22 03:09

dongkang