我正在制作相机应用程序,但不断收到此错误Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
。这是发生错误guard let device = try? activeInput.device else {return}
class ViewController: UIViewController, AVCapturePhotoCaptureDelegate, AVCaptureFileOutputRecordingDelegate {
private let movieOutput = AVCaptureMovieFileOutput()
private var previewLayer: AVCaptureVideoPreviewLayer!
private var activeInput: AVCaptureDeviceInput!
private var outputURL: URL!
private var videoDataOutput: AVCaptureVideoDataOutput!
func startRecording() {
if movieOutput.isRecording == false {
let connection = videoDataOutput.connection(with: AVMediaType.video)
if (connection?.isVideoOrientationSupported)! {
connection?.videoOrientation = currentVideoOrientation()
}
if (connection?.isVideoStabilizationSupported)! {
connection?.preferredVideoStabilizationMode = AVCaptureVideoStabilizationMode.auto
}
guard let device = try? activeInput.device else {return}
if (device.isSmoothAutoFocusSupported) {
do {
try device.lockForConfiguration()
device.isSmoothAutoFocusEnabled = false
device.unlockForConfiguration()
} catch {
print("Error setting configuration: \(error)")
}
}
outputURL = tempURL()
movieOutput.startRecording(to: outputURL, recordingDelegate: self)
}
else {
stopRecording()
}
}
}