PHAssetResourceRequestOptions progressHandler/PHAssetResourceProgressHandler

来源:互联网 发布:岳云鹏 于谦 知乎 编辑:程序博客网 时间:2024/06/05 15:54
fileprivate func dowloadAsset(_ asset: PHAsset, to url: URL, completion handle:@escaping (()->())) {        if #available(iOS 9.1, *) {            if asset.mediaType == .image && asset.mediaSubtypes == .photoLive {                let options = PHLivePhotoRequestOptions()                options.isNetworkAccessAllowed = true                self.imageManager.requestLivePhoto(for: asset, targetSize: CGSize.zero, contentMode: .aspectFill, options: options, resultHandler: { (livePhoto, info) in                    if let dic = info {                        if let error = dic[PHImageErrorKey] {                            print(error)                            return                        }else {                            let livePhotoData = NSKeyedArchiver.archivedData(withRootObject: livePhoto!)                            if FileManager.default.createFile(atPath: url.path, contents: livePhotoData, attributes: nil) {                                print(url.path)                                handle()                            }else {                                return                            }                        }                    }else {                        return                    }                })            }        }        if asset.mediaType == .image {            let options = PHImageRequestOptions()            options.isNetworkAccessAllowed = true            self.imageManager.requestImageData(for: asset, options: options, resultHandler: { (imageData, dataUTI, orientation, info) in                if let dic = info {                    if let error = dic[PHImageErrorKey] {                        print(error)                        return ;                    }else {                        if FileManager.default.createFile(atPath: url.path, contents: imageData, attributes: nil) {                            print(url.path)                            handle()                        }else {                            return                        }                    }                }            })        }                if #available(iOS 9.0, *) {            if asset.mediaType == .video {                let options = PHVideoRequestOptions()                options.isNetworkAccessAllowed = true                self.imageManager.requestExportSession(forVideo: asset, options: options, exportPreset: AVAssetExportPresetHighestQuality, resultHandler: { (session, info) in                    if let dic = info {                        if let error = dic[PHImageErrorKey] {                            print(error)                            return                        }else {                            session?.outputURL = url                            let resources = PHAssetResource.assetResources(for: asset)                            for resource in resources {                                session?.outputFileType = resource.uniformTypeIdentifier                                if let _ = session?.outputFileType {                                    break                                }                            }                            session?.exportAsynchronously(completionHandler: {                                 if session?.status == .completed {                                    print(url.path)                                    handle()                                }                            })                        }                    }                })            }        }    }

以上是Swift3.0版本的,self.imageManager是PHImageManager.default

- (void)downloadAsset:(PHAsset *)asset toURL:(NSURL *)url completion:(void (^)(void))completion{    if (asset.mediaType == PHAssetMediaTypeImage && (asset.mediaSubtypes & PHAssetMediaSubtypePhotoLive))    {        PHLivePhotoRequestOptions *options = [PHLivePhotoRequestOptions new];        options.networkAccessAllowed = YES;        [[PHImageManager defaultManager] requestLivePhotoForAsset:asset targetSize:CGSizeZero contentMode:PHImageContentModeAspectFill options:options resultHandler:^(PHLivePhoto * _Nullable livePhoto, NSDictionary * _Nullable info) {            if ([info objectForKey:PHImageErrorKey] == nil)            {                NSData *livePhotoData = [NSKeyedArchiver archivedDataWithRootObject:livePhoto];                if ([[NSFileManager defaultManager] createFileAtPath:url.path contents:livePhotoData attributes:nil])                {                    NSLog(@"downloaded live photo:%@", url.path);                    completion();                }            }        }];    }    else if (asset.mediaType == PHAssetMediaTypeImage)    {        PHImageRequestOptions *options = [PHImageRequestOptions new];        options.networkAccessAllowed = YES;        [[PHImageManager defaultManager] requestImageDataForAsset:asset options:options resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {            if ([info objectForKey:PHImageErrorKey] == nil                && [[NSFileManager defaultManager] createFileAtPath:url.path contents:imageData attributes:nil])            {                NSLog(@"downloaded photo:%@", url.path);                completion();            }        }];    }    else if (asset.mediaType == PHAssetMediaTypeVideo)    {        PHVideoRequestOptions *options = [PHVideoRequestOptions new];        options.networkAccessAllowed = YES;        [[PHImageManager defaultManager] requestExportSessionForVideo:asset options:options exportPreset:AVAssetExportPresetHighestQuality resultHandler:^(AVAssetExportSession * _Nullable exportSession, NSDictionary * _Nullable info) {            if ([info objectForKey:PHImageErrorKey] == nil)            {                exportSession.outputURL = url;                NSArray<PHAssetResource *> *resources = [PHAssetResource assetResourcesForAsset:asset];                for (PHAssetResource *resource in resources)                {                    exportSession.outputFileType = resource.uniformTypeIdentifier;                    if (exportSession.outputFileType != nil)                        break;                }                [exportSession exportAsynchronouslyWithCompletionHandler:^{                    if (exportSession.status == AVAssetExportSessionStatusCompleted)                    {                        NSLog(@"downloaded video:%@", url.path);                        completion();                    }                }];            }        }];    }}
这是OC版本的,注意每个API的系统要求,比如live照片需要iOS9.1以上

6 0
原创粉丝点击