修剪后无法在相机胶卷中显示视频

时间:2017-06-26 10:29:11

标签: swift swift3 avfoundation phphotolibrary ios10.3

我正在开发具有修剪功能的视频播放器。我正在使用ABVideoRangeSlider。每当我尝试修剪和保存视频时,我都会收到Error Domain=NSCocoaErrorDomain Code=-1 "(null)"此错误。 这是我的代码片段

    let composition = AVMutableComposition()
    let track = composition.addMutableTrack(withMediaType: AVMediaTypeVideo, preferredTrackID:Int32(kCMPersistentTrackID_Invalid))

    try! track.insertTimeRange(CMTimeRangeMake(startTimeForCurrentSlice, endTimeForCurrentSlice), of: asset.tracks(withMediaType: AVMediaTypeVideo)[0] as AVAssetTrack, at: kCMTimeZero)
    var paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
    let documentsDirectory = paths[0] as String
    let videoPathToSave = documentsDirectory.appending("mergeVideo-\(arc4random()%1000)-d.mp4")
    let videoURLToSave = NSURL(fileURLWithPath: videoPathToSave)

    PHPhotoLibrary.shared().performChanges({
        PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: videoURLToSave as URL)
    }) { saved, error in
        if saved {
            let alertController = UIAlertController(title: "Your video was successfully saved", message: nil, preferredStyle: .alert)
            let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil)
            alertController.addAction(defaultAction)
            self.present(alertController, animated: true, completion: nil)
        } else {
            print("Error: \(String(describing: error!))")
        }
    }

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

Before saving to camera roll You need to export your AVMutableComposition with AVAssetExportSession or with AVAssetWriter, and after that You can save it to camera roll.

Checkout this教程,用于AVAssetExportSession用法。要保存到相机胶卷,它会使用ALAssetsLibrary,但您可以使用Photos保存它。