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
- PHAssetResourceRequestOptions progressHandler/PHAssetResourceProgressHandler
- ipython文档路径配置
- 《MySQL必知必会》学习笔记
- echart,折线图,两条线之间的阴影
- CodeForces - 404A: Valera and X
- Lucene 6.0 提取新闻关键词Top-N
- PHAssetResourceRequestOptions progressHandler/PHAssetResourceProgressHandler
- HDU-1059 Dividing (多重背包)
- 决策树算法
- 安卓中为了获取context的方法和区别
- 20161230C++阶段班03_Object C++_03构造函数/命名空间/初始化列
- Spring MVC---基于注解的控制器
- mac提示Operation not permitted的解决方法和安装MAMP集成环境后使用mysql命令的问题
- openfire+smack添加好友申请及好友对申请响应监听
- 对于微电子学初学者应该掌握哪些能力与技