在运行iOS 12.1.2的iPhone XS MAX上,将此代码与Swift中的内置WideAngleCamera一起使用:

let lensPos: Float  = AVCaptureDevice.currentLensPosition;
lockCameraForSettings();
self.inputDevice?.setFocusModeLocked(lensPosition: LensPos, completionHandler: { (time) -> Void in})
unlockCameraForShooting();

导致崩溃:
[AVCaptureDevice setFocusModeLockedWithLensPosition:completionHandler:]传递的lensPosition-3402823463852885981170418348451695140000000超出范围[0,1]
摄像机正在运行,并在屏幕预览上清晰可见。它怎么可能处于这种配置中?
在0-1之间插入一个常量是可行的,至少它不会抛出错误。

最佳答案

我相信你的意思是用.lensPosition而不是.currentLensPosition来表示镜头的位置。只有在引用.lensPosition类型的实例时,才能访问AVCaptureDevice

    var captureDevice: AVCaptureDevice?

    // Plus models and X's
    if let device = AVCaptureDevice.default(.builtInDualCamera,
                                            for: .video, position: .back) {
        captureDevice = device
     // Single Lens devices.
    } else if let device = AVCaptureDevice.default(.builtInWideAngleCamera,
                                                   for: .video, position: .back) {
        captureDevice = device
    } else {
        // No camera was found, is it broke?
        print("Missing expected back camera device.")
    }

    if let device = captureDevice {
        // We have a device, do something with it.
        print(device.lensPosition)
    }

10-06 07:01