AVCaptureDevice videoZoomFactor 双指缩放率

AVCaptureDevice videoZoomFactor Pinch-to-Zoom Rate

我正在尝试在 AVFoundation[=21] 中使用 AVCaptureDevice 实现“双指缩放”功能=]:

 @IBAction func pinchGestureDetected(_ gestureRecognizer: UIPinchGestureRecognizer)  {

    switch gestureRecognizer.state {
    case .began:
        print ("began")
        self.currenZoomFactor = self.videoDevice!.videoZoomFactor
        do {
            try self.videoDevice!.lockForConfiguration()
        } catch let error as NSError {
            NSLog("Could not lock device for configuration: %@", error)
        }

    case .changed:
        print ("changed")

            var zoomValue : CGFloat = ((gestureRecognizer.scale) - 1) + self.currenZoomFactor
            if zoomValue > min(10.00, self.videoDevice!.activeFormat.videoMaxZoomFactor) {
                zoomValue = min(10.00, self.videoDevice!.activeFormat.videoMaxZoomFactor)

            } else if zoomValue < 1.00 {
                zoomValue = 1.00
            }

            self.videoDevice!.videoZoomFactor = sentZoomValue

    case .ended, .cancelled:
        print ("ended/canceld")
        self.videoDevice!.unlockForConfiguration()

    default:
        break
    }

}

以上工作正常。但是,对于上述内容,缩放率是线性的 缩放比例。这使得在更高的缩放因子下缩放更慢。

如何在更高的缩放系数下获得加速的缩放率?

要获得加速的缩放率,我们需要进行以下计算。

您可以从您的 pinchGestureDetected

中调用此实用程序方法
func zoomto(scale: CGFloat, hasBegunToZoom: Bool) {

    if hasBegunToZoom {
        initialPinchZoom = captureDevice.videoZoomFactor
    }
    do {
        try captureDevice.lockForConfiguration()
        if scale < 1.0 {
            zoomFactor = initialPinchZoom - pow(captureDevice.activeFormat.videoMaxZoomFactor, 1.0 - scale)
        }
        else {
            zoomFactor = initialPinchZoom + pow(captureDevice.activeFormat.videoMaxZoomFactor, (scale - 1.0f) / 2.0f)
        }
        zoomFactor = min(10.0, zoomFactor)
        zoomFactor = max(1.0, zoomFactor)
        captureDevice.videoZoomFactor = zoomFactor
        captureDevice.unlockForConfiguration()
    } catch let error as NSError {
        NSLog("Could not lock device for configuration: %@", error)
    }
}

您可以像下面这样调用

@IBAction func pinchGestureDetected(_ gestureRecognizer: UIPinchGestureRecognizer)  {
    zoomto(scale: gestureRecognizer.scale, hasBegunToZoom:(gestureRecognizer.state == .began))
}