检测相机何时失去焦点(快速)

时间:2018-07-15 22:40:22

标签: ios swift camera autofocus avkit

我正在构建全屏摄像头应用程序,并且允许用户通过点击屏幕来设置兴趣点。但是,当用户移动相机并且视图失去焦点时,我该如何分辨?我想将设备的focusMode切换为AVCaptureDevice.FocusMode.continuousAutoFocus,这就是我的代码的样子就像现在一样。

@objc func handleTap(tap: UITapGestureRecognizer) {
    let location = tap.location(in: view)

    var focusPoint = CGPoint()
    focusPoint.x = location.x / view.frame.width
    focusPoint.y = location.y / view.frame.height

    if let device = currentVideoDevice {
        if try! (currentVideoDevice?.lockForConfiguration()) != nil {
            if device.isFocusPointOfInterestSupported {
                device.focusPointOfInterest = focusPoint
                device.focusMode = AVCaptureDevice.FocusMode.autoFocus
            }
            if device.isExposurePointOfInterestSupported {
                device.exposurePointOfInterest = focusPoint
                device.exposureMode = AVCaptureDevice.ExposureMode.autoExpose
            }
            device.unlockForConfiguration()
        }
    }
}

0 个答案:

没有答案