导出相册视频-> 本地沙盒

 

慢动作视频, 特殊处理 https://www.jianshu.com/p/bbae60b21422

          https://www.jianshu.com/p/4ec00b289885 

1, exportSession

let option = PHVideoRequestOptions()
                option.isNetworkAccessAllowed = true
                option.deliveryMode = .highQualityFormat
                PHCachingImageManager.default().requestExportSession(forVideo: asset, options: option, exportPreset: AVAssetExportPresetHighestQuality) { (exportSession, info) in
                    guard let exportSession = exportSession else {
                        single(.error(KimPhotoHelperError.sourceNotFound))
                        return
                    }
                    exportSession.outputURL = videoPath
                    exportSession.outputFileType = .mp4
                    exportSession.exportAsynchronously {
                        switch exportSession.status {
                        case .completed:
                            single(.success((videoPath.relativePath, size)))
                        case .failed:
                            print(exportSession.error)
                            single(.error(KimPhotoHelperError.sourceNotFound))
                        default:
                            break
                        }
                    }
                }

 

2,  PHAssetResourceManager

let resourceManager = PHAssetResourceManager.default()
                let option = PHAssetResourceRequestOptions()
                option.isNetworkAccessAllowed = true
                resourceManager.writeData(for: assetRescource, toFile: videoPath, options: option) { (error) in
                    guard let error = error else {
                        //single(.error(e))
                        exportSession(asset)
                        return
                    }
                    single(.success((videoPath.relativePath, size)))
                }

 3,  先 requestDatqa -> 再 writeData

let resourceManager = PHAssetResourceManager.default()
                  let option = PHAssetResourceRequestOptions()
                  option.isNetworkAccessAllowed = true
                  var videoData = Data()
                  resourceManager.requestData(for: assetRescource, options: option, dataReceivedHandler: { (data) in
                      videoData.append(data)
                  }) { (error) in
                      if let error = error {
                          print(error)
                          exportSession(asset)
  //                        single(.error(KimPhotoHelperError.sourceNotFound))
                      } else {
                          do {
                              try videoData.write(to: videoPath)
                          } catch {
                              
                          }
                          single(.success((videoPath.relativePath, size)))
                      }
                      
                  }

 

4, 导出慢动作视频 AVCompostion 类型

func ConvertAvcompositionToAvasset(avComp: AVComposition, completion:@escaping (_ avasset: AVAsset) -> Void){
        let exporter = AVAssetExportSession(asset: avComp, presetName: AVAssetExportPresetHighestQuality)
        let randNum:Int = Int(arc4random())
        //Generating Export Path
        let exportPath: NSString = NSTemporaryDirectory().appendingFormat("\(randNum)"+"video.mov") as NSString
        let exportUrl: NSURL = NSURL.fileURL(withPath: exportPath as String) as NSURL
        //SettingUp Export Path as URL
        exporter?.outputURL = exportUrl as URL
        exporter?.outputFileType = AVFileTypeQuickTimeMovie
        exporter?.shouldOptimizeForNetworkUse = true
        exporter?.exportAsynchronously(completionHandler: {() -> Void in
            DispatchQueue.main.async(execute: {() -> Void in
                if exporter?.status == .completed {
                    let URL: URL? = exporter?.outputURL
                    let Avasset:AVAsset = AVAsset(url: URL!)
                    completion(Avasset)
                }
                else if exporter?.status == .failed{
                    print("Failed")

                }
            })
        }) }

 

posted @ 2020-04-30 14:38  Da雪山  阅读(696)  评论(0编辑  收藏  举报