AVCaptureDeviceInput 在解开可选值时意外发现 nil



我正在尝试获取捕获设备的图像,但它一直返回 nil if let input...,我找不到解决方案。

它试图捕获错误,但它立即兑现。

let backCamera = AVCaptureDevice.default(for: AVMediaType.video)
    do {
        if let input = try AVCaptureDeviceInput(device: backCamera!) as? AVCaptureInput {
            if (captureSession?.canAddInput(input))! {
                captureSession?.addInput(input)
                stillImageOutput = AVCapturePhotoOutput()
                let settings = AVCapturePhotoSettings()
                let settingsFormat = [AVVideoCodecKey : AVVideoCodecType.jpeg]
                settings.previewPhotoFormat = settingsFormat
                stillImageOutput?.capturePhoto(with: settings, delegate: self as! AVCapturePhotoCaptureDelegate)
                if (captureSession?.canAddOutput(stillImageOutput!))! {
                    captureSession?.addOutput(stillImageOutput!)
                    previewLayer = AVCaptureVideoPreviewLayer(session: captureSession!)
                    previewLayer?.videoGravity = AVLayerVideoGravity.resizeAspect
                    previewLayer?.connection?.videoOrientation = AVCaptureVideoOrientation.portrait
                    cameraView.layer.addSublayer(previewLayer!)
                    captureSession?.startRunning()
                }
            }
        }

    } catch let error as NSError {
        print("Error: (error)")
    }

导致崩溃的不是你的错误抛出 - 这是你所有的强制解包。 backCameracaptureSessionstillImageOutputpreviewLayer 都是Optional,但你每次都强制打开它们。 为什么不使用保护语句或可选绑定来避免这些崩溃? 最终,如果这些项为 nil,您将无法运行任何代码,因此我会像这样重构您的代码:

//Use guard to make sure you have a non-nil captureSession and a default device for .video
guard let captureSession = captureSession, let backCamera = AVCaptureDevice.default(for: AVMediaType.video) else { return }
do {
    if let input = try AVCaptureDeviceInput(device: backCamera) as? AVCaptureInput {
        if (captureSession.canAddInput(input)) {
            captureSession.addInput(input)
            stillImageOutput = AVCapturePhotoOutput()
            //Since stillImageOutput is an Optional I'm putting in another guard just to avoid having to deal with it as an Optional
            guard let stillImageOutput = stillImageOutput else { return }
            let settings = AVCapturePhotoSettings()
            let settingsFormat = [AVVideoCodecKey : AVVideoCodecType.jpeg]
            settings.previewPhotoFormat = settingsFormat
            stillImageOutput.capturePhoto(with: settings, delegate: self as! AVCapturePhotoCaptureDelegate)
            if (captureSession.canAddOutput(stillImageOutput)) {
                captureSession.addOutput(stillImageOutput)
                previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
                //Same as above - since you declared previewLayer as Optional I'll guard so I don't have to deal with it as an Optional
                guard let previewLayer = previewLayer else { return }
                previewLayer.videoGravity = AVLayerVideoGravity.resizeAspect
                previewLayer.connection?.videoOrientation = AVCaptureVideoOrientation.portrait
                cameraView.layer.addSublayer(previewLayer)
                captureSession.startRunning()
            }
        }
    }
} catch let error {
    print("Error: (error)")
}

老实说,其中一些有点矫枉过正,所以这取决于一些个人喜好,但是像AVCaptureDevice.default...这样的东西,它真的可能是或可能不会为零,这取决于实际设备,你可能不想让它崩溃如果它不存在。

相关内容

  • 没有找到相关文章

最新更新