@owjhart: Great solution!!!
For those, who would like to grab life photos as movie files, I've adopted the code a bit :-)
- (void)downloadAsset:(PHAsset *)pAsset
completion:(void (^)(BOOL pSucceeded, NSURL* pURL))pCompletion {
NSParameterAssert(pCompletion);
if ((PHAssetMediaTypeImage == pAsset.mediaType) &&
(pAsset.mediaSubtypes & PHAssetMediaSubtypePhotoLive)) {
NSArray* assetResources = [PHAssetResource assetResourcesForAsset:pAsset];
PHAssetResource* assetResource = nil;
for (PHAssetResource* asress in assetResources) {
if (UTTypeConformsTo((__bridge CFStringRef)asress.uniformTypeIdentifier, kUTTypeMovie)) {
assetResource = asress;
break;
}
}
if (assetResource) {
__block NSMutableData* assetData = NSMutableData.new;
[PHAssetResourceManager.defaultManager requestDataForAssetResource:assetResource
options:nil
dataReceivedHandler:^(NSData * _Nonnull pData) {
[assetData appendData:pData];
}
completionHandler:^(NSError * _Nullable pError) {
//kUTTypeLivePhoto
CFStringRef extension = UTTypeCopyPreferredTagWithClass((__bridge CFStringRef)assetResource.uniformTypeIdentifier, kUTTagClassFilenameExtension);
NSString* filename = [NSString stringWithFormat:@"%@.%@", NSProcessInfo.processInfo.globallyUniqueString, extension];
NSURL* tempURL = [[NSURL fileURLWithPath:NSTemporaryDirectory()
isDirectory:YES] URLByAppendingPathComponent:filename];
if ((!pError) &&
(assetData.length) &&
([NSFileManager.defaultManager createFileAtPath:tempURL.path
contents:assetData
attributes:nil])) {
pCompletion(YES, tempURL);
}
else {
pCompletion(NO, nil);
}
}];
}
else {
pCompletion(NO, nil);
}
}
else if (PHAssetMediaTypeImage == pAsset.mediaType) {
PHImageRequestOptions* options = PHImageRequestOptions.new;
options.networkAccessAllowed = YES;
[PHImageManager.defaultManager requestImageDataForAsset:pAsset
options:options
resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
CFStringRef extension = UTTypeCopyPreferredTagWithClass((__bridge CFStringRef)dataUTI, kUTTagClassFilenameExtension);
NSString* filename = [NSString stringWithFormat:@"%@.%@", NSProcessInfo.processInfo.globallyUniqueString, (__bridge NSString*)extension];
NSURL* tempURL = [[NSURL fileURLWithPath:NSTemporaryDirectory()
isDirectory:YES] URLByAppendingPathComponent:filename];
if ((nil == [info objectForKey:PHImageErrorKey]) &&
([NSFileManager.defaultManager createFileAtPath:tempURL.path
contents:imageData
attributes:nil])) {
NSLog(@"downloaded photo:%@", tempURL.path);
pCompletion(YES, tempURL);
}
else {
pCompletion(NO, nil);
}
}];
}
else if (PHAssetMediaTypeVideo == pAsset.mediaType) {
PHVideoRequestOptions* options = PHVideoRequestOptions.new;
options.networkAccessAllowed = YES;
[PHImageManager.defaultManager requestExportSessionForVideo:pAsset
options:options
exportPreset:AVAssetExportPresetHighestQuality
resultHandler:^(AVAssetExportSession * _Nullable exportSession, NSDictionary * _Nullable info) {
if (nil == [info objectForKey:PHImageErrorKey]) {
NSArray<PHAssetResource*>* resources = [PHAssetResource assetResourcesForAsset:pAsset];
for (PHAssetResource* resource in resources) {
exportSession.outputFileType = resource.uniformTypeIdentifier;
if (nil != exportSession.outputFileType) {
break;
}
}
CFStringRef extension = UTTypeCopyPreferredTagWithClass((__bridge CFStringRef)exportSession.outputFileType, kUTTagClassFilenameExtension);
NSString* filename = [NSString stringWithFormat:@"%@.%@", NSProcessInfo.processInfo.globallyUniqueString, (__bridge NSString*)extension];
NSURL* tempURL = [[NSURL fileURLWithPath:NSTemporaryDirectory()
isDirectory:YES] URLByAppendingPathComponent:filename];
exportSession.outputURL = tempURL;
[exportSession exportAsynchronouslyWithCompletionHandler:^{
if (AVAssetExportSessionStatusCompleted == exportSession.status) {
NSLog(@"downloaded video:%@", tempURL.path);
pCompletion(YES, tempURL);
}
else {
pCompletion(NO, nil);
}
}];
}
else {
pCompletion(NO, nil);
}
}];
}
else {
pCompletion(NO, nil);
}
}