如何撤消点击以聚焦/计量?

时间:2017-02-21 08:14:46

标签: ios autofocus avcapturedevice avcapture

在我的相机应用程序中,我想要支持水龙头对焦和计量。我发现,作为一个用户,我的手指有时会偶然触摸某个地方的屏幕,并且它聚焦在那里,所以我想要撤消它。在我的应用程序中,我正在考虑添加一个重置焦点按钮,它可以撤消点击以进行聚焦:它会告诉iOS在它认为最好的情况下进行聚焦(就像点击聚焦之前一样)。

iOS是否为此提供API?

当用户点击一个点时,我可以在AVCaptureDevice中指定focusPointOfInterest和exposurePointOfInterest。但我没有看到函数clearFocusPointOfInterest()和clearExposurePointOfInterest()。我该怎么做?

1 个答案:

答案 0 :(得分:0)

您需要将焦点设置为.continousAutoFocus,将曝光设置为.continuousAutoExposure,将焦点设置为CGPoint(x:0.5,y:0.5)。以下聚焦和自动聚焦代码对我有用。

    @IBAction private func doFocusAndExpose(_ gestureRecognizer: UITapGestureRecognizer) {
        let devicePoint = previewView.videoPreviewLayer.captureDevicePointConverted(fromLayerPoint: gestureRecognizer.location(in: gestureRecognizer.view))
        focus(with: .autoFocus, exposureMode: .autoExpose, at: devicePoint)
    }

    @IBAction private func doAutofocus(_ gestureRecognizer: UITapGestureRecognizer) {
        let devicePoint = CGPoint(x:0.5,y:0.5)
        focus(with: .continuousAutoFocus, exposureMode: .continuousAutoExposure, at: devicePoint)
    }

    private func focus(with focusMode: AVCaptureDevice.FocusMode,
                       exposureMode: AVCaptureDevice.ExposureMode,
                       at devicePoint: CGPoint) {

        sessionQueue.async {
            let device = self.videoDeviceInput.device
            do {
                try device.lockForConfiguration()

                if device.isFocusPointOfInterestSupported && device.isFocusModeSupported(focusMode) {
                    device.focusPointOfInterest = devicePoint
                    device.focusMode = focusMode
                }

                if device.isExposurePointOfInterestSupported && device.isExposureModeSupported(exposureMode) {
                    device.exposurePointOfInterest = devicePoint
                    device.exposureMode = exposureMode
                }

                device.unlockForConfiguration()
            } catch {
                print("Could not lock device for configuration: \(error)")
            }
        }
    }