我有一个功能,我想用它来保存视频到相机胶卷。出于某种原因,在我停止录制后,没有任何反应?它不会将其保存到相机胶卷或除
之外的任何其他错误 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
}
答案 0 :(得分:1)
您在致电stopRecording
后过早访问该视频文件。正如在here上提到的那样,你应该只尝试使用capture(_:didFinishRecordingToOutputFileAt:fromConnections:error:)
委托回调中的URL,因为一旦你要求它停止,记录需要完成对文件的任何写入。
您需要将照片库调用移动到该委托方法中:
func capture(_ captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAt outputFileURL: URL!, fromConnections connections: [Any]!, error: Error!) {
// PHPhotoLibrary.shared().performChanges...
}