-1

Following the repo and tutorial: https://github.com/brianadvent/CustomCamera

I am getting error with the following code: Error:

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()
        }

Similarly, with the following segment

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)



        }

Error associated with second code snippet: Initializer for conditional binding must have Optional type, not 'AVCaptureVideoPreviewLayer'

low_queue
  • 65
  • 3
  • You're trying to unwrap something that isn't an optional. Just don't unwrap it, and directly assign it to a variable – Alexander Jul 19 '19 at 03:11

2 Answers2

0

Make the below change.

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

Hope you get the point.

devices(an array) was not an optional, that is, you will surely get the array. But, devices.firstis an optional, because the array might be empty.

Roohul
  • 1,009
  • 1
  • 15
  • 26
0

You're trying to unwrap something that isn't an optional. Just don't unwrap it, and directly assign it to a variable, as you did with dataOutput.

Alexander
  • 59,041
  • 12
  • 98
  • 151