2016-12-16 54 views
2

我有一個功能,我想用它來保存視頻到相機膠捲。出於某種原因,在我停止錄製後,什麼都沒有發生?它不會將它保存到相機膠捲或者讓其他任何錯誤比將視頻保存到相機膠捲時出錯? [斯威夫特]

Error Domain=NSCocoaErrorDomain Code=-1 "(null)"

這裏是我的代碼:

@IBAction func record(_ sender: UIButton) { 

    if recordingInProgress{ 
     output.stopRecording() 
     recordingLabel.textColor = UIColor.rgb(red: 173, green: 173, blue: 173) 
     PHPhotoLibrary.shared().performChanges({ 
      PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: self.outputURL 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{ 
     let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString 
     let outputPath = "\(documentsPath)/output.mov" 
     outputURL = NSURL(fileURLWithPath: outputPath) 
     output.startRecording(toOutputFileURL: outputURL as URL!, recordingDelegate: self) 
     recordingLabel.textColor = UIColor.rgb(red: 250, green: 3, blue: 33) 
    } 

    recordingInProgress = !recordingInProgress 


} 

回答

1

要調用stopRecording後立即訪問視頻文件。正如here中所述,您應該只嘗試使用capture(_:didFinishRecordingToOutputFileAt:fromConnections:error:)委託回調中的URL,因爲錄製需要在您要求停止對該文件的任何寫入之前完成寫入。

你會想你的照片庫呼叫轉移到該委託方法:

func capture(_ captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAt outputFileURL: URL!, fromConnections connections: [Any]!, error: Error!) { 
    // PHPhotoLibrary.shared().performChanges... 
}