简体   繁体   中英

Errors building a camera in an IOS app

I am trying to build camera functionality in an IOS app. Here is my first error:

Cannot assign value of type 'AVCaptureVideoPreviewLayer' to type 'CALayer!'

Swift also has an issue with assigning a property to itself

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

It's not compiling because that first line isn't a valid if statement:

if previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)

Even an if-let wouldn't be valid here because that initializer isn't failable. Without more context, I suggest you drop that if entirely and assign the AVCaptureVideoPreviewLayer right to your previewLayer like so:

self.previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM