我如何撤消点击以聚焦/测光?

问题描述 投票:1回答:1

在我的相机应用中,我正在考虑支持点击以进行对焦和测光。作为用户,我发现我的手指有时不小心触摸了屏幕,并且在那儿聚焦,因此我想撤消它。在我的应用程序中,我正在考虑添加一个“重置焦点”按钮,该按钮将撤消轻按以进行聚焦:它将告诉iOS在其认为最佳的位置进行聚焦(就像在轻按以进行聚焦之前一样)。

iOS是否为此提供API?

当用户点击某个点时,我可以在AVCaptureDevice中分配给focusPointOfInterest和ExposurePointOfInterest。但是我看不到函数clearFocusPointOfInterest()和clearExposurePointOfInterest()。我该怎么做?

ios autofocus avcapturedevice avcapture
1个回答
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)")
            }
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.