我正尝试在 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
}
}
以上工作正常。但是,如上所述,缩放率是线性,具有缩放比例。这使得缩放系数较高时缩放速度要慢得多。
如何在更高的缩放系数下获得加速变焦率?
答案 0 :(得分:2)
要获得加速缩放率,我们需要进行一些以下计算。
您可以从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))
}