为什么我的AVCaptureDeviceInput出现致命错误?

时间:2018-09-26 01:33:48

标签: swift avfoundation avcapturedevice

我正在制作相机应用程序,但不断收到此错误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()
    }

 }
}

0 个答案:

没有答案