条件绑定的初始化程序必须具有可选类型:不是'[AVCaptureDevice]'

时间:2019-07-19 02:56:08

标签: ios swift iphone

按照存储库和教程进行:https://github.com/brianadvent/CustomCamera

以下代码出现错误: 错误:

Initializer for conditional binding must have Optional type, not 'AVCaptureVideoPreviewLayer'
      if let availableDevices = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaType.video, position: .back).devices {
            captureDevice = availableDevices.first
            beginSession()
        }

类似地,具有以下段

if let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) {
            self.previewLayer = previewLayer
            self.view.layer.addSublayer(self.previewLayer)
            self.previewLayer.frame = self.view.layer.frame
            captureSession.startRunning()

            let dataOutput = AVCaptureVideoDataOutput()
            dataOutput.videoSettings = [(kCVPixelBufferPixelFormatTypeKey as NSString):NSNumber(value:kCVPixelFormatType_32BGRA)] as [String : Any]

            dataOutput.alwaysDiscardsLateVideoFrames = true

            if captureSession.canAddOutput(dataOutput) {
                captureSession.addOutput(dataOutput)
            }

            captureSession.commitConfiguration()


            let queue = DispatchQueue(label: "com.brianadvent.captureQueue")
            dataOutput.setSampleBufferDelegate(self, queue: queue)



        }

与第二个代码段相关的错误: 条件绑定的初始化程序必须具有Optional类型,而不是'AVCaptureVideoPreviewLayer'

2 个答案:

答案 0 :(得分:0)

进行以下更改。

if let availableDevice = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaType.video, position: .back).devices.first {
        captureDevice = availableDevice
        beginSession()
    }

希望您明白这一点。

devices(一个数组)不是可选的,也就是说,您一定会得到该数组。但是,devices.first是可选的,因为该数组可能为空。

答案 1 :(得分:0)

您正试图打开一些非可选的东西。只是不要解开包装,而是像使用dataOutput一样直接将其分配给变量。