gpt4 book ai didi

ios - AVCaptureDevice videoZoomFactor 双指缩放率

转载 作者:搜寻专家 更新时间:2023-11-01 07:11:44 24 4
gpt4 key购买 nike

我正在尝试使用 AVFoundation 中的 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))
}

关于ios - AVCaptureDevice videoZoomFactor 双指缩放率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44530460/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com