在IOS应用中构建相机时出错

时间:2018-07-12 14:33:51

标签: ios swift camera

我正在尝试在IOS应用程序中构建相机功能。这是我的第一个错误:

无法将类型为“ AVCaptureVideoPreviewLayer”的值分配为类型为“ CALayer!”

Swift在为其分配属性时也存在问题

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

1 个答案:

答案 0 :(得分:1)

未编译,因为第一行不是有效的if语句:

if previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)

即使if-let在这里也不是有效的,因为that initializer不会失败。在没有更多上下文的情况下,建议您完全舍弃它,然后将AVCaptureVideoPreviewLayer的权限分配给您的previewLayer,如下所示:

self.previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)