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