【发布时间】:2020-07-02 02:06:00
【问题描述】:
我正在构建一个自定义相机,我希望允许用户在超广角相机、广角相机和长焦相机之间切换,就像默认的相机应用程序一样,但我遇到了一些奇怪的问题,我会尝试在下面显示:
第一张图片是使用 iOS 默认相机应用拍摄的,第二张是使用我的自定义相机应用拍摄的。在我看来,当我从广角相机切换到超广角相机时,我的AVCaptureVideoPreviewLayer 没有正确安装。问题是:我不确定我应该怎么做,所以你们能给我一个关于如何解决它的线索吗?
我还有一个视频,它显示了我的自定义应用程序中相机之间的转换,但我无法在此处上传。
这是我在相机之间切换的代码部分:
if let device = AVCaptureDevice.default(.builtInUltraWideCamera, for: .video, position: .back) {
currentCameraDevice = device
changeCameraDevice()
}
}
private func changeCameraDevice() {
guard captureSession.isRunning else { return }
captureSession.beginConfiguration()
guard let view = (view as? UIViewController)?.view else { return }
guard let device = currentCameraDevice else { return }
guard let input = try? AVCaptureDeviceInput(device: device) else { return }
captureSession.inputs.forEach { captureSession.removeInput($0) }
captureSession.addInput(input)
captureSession.commitConfiguration()
captureSession.startRunning()
viewDidLayoutSubviews(view)
}
这里是我将预览层插入视图层堆栈的地方:
view.layer.insertSublayer(layer, at: index)
其中layer 是AVCaptureVideoPreviewLayer
【问题讨论】: