requestImageDataForAsset with video PHAsset returns with imageData pointing a photo

941 Views Asked by At

iOS9.3beta3,

PHImageManager requestImageDataForAsset returns with imageData pointing to a photo although the PHAssert is a video asset which was captured on the device.

Documentation says:

"requestImageDataForAsset(_:options:resultHandler:)

... If the version option is set to PHImageRequestOptionsVersionCurrent, Photos provides rendered image data, including the results of any edits that have been made to the asset content. Otherwise, Photos provides the originally captured image data for the asset."

Is that a bug in iOS9.3 ?

amir.

1

There are 1 best solutions below

0
On

Simple solution for me

+ (void)getAssetData:(NSString *)ident completeBlock:(void (^)(NSData *assetData))completeBlock {
    PHFetchResult *fetchResult = [PHAsset fetchAssetsWithLocalIdentifiers:@[ident] options:nil];
    if (fetchResult && [fetchResult count] == 0) {
        if (completeBlock) {
            completeBlock(nil);
        }
        return;
    }

    PHAsset *asset = fetchResult.firstObject;
    if (asset.mediaType == PHAssetMediaTypeVideo) {
        [[PHImageManager defaultManager] requestAVAssetForVideo:asset options:nil resultHandler:^(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info) {
            if ([asset isKindOfClass:[AVURLAsset class]]) {
                AVURLAsset* urlAsset = (AVURLAsset*)asset;
                NSNumber *size;
                [urlAsset.URL getResourceValue:&size forKey:NSURLFileSizeKey error:nil];
                NSData *data = [NSData dataWithContentsOfURL:urlAsset.URL];
                completeBlock(data);
            } else {
                completeBlock(nil);
            }
        }];
    } else {
        [[PHImageManager defaultManager] requestImageDataForAsset:fetchResult.firstObject options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
            completeBlock(imageData);
        }];
    }
}