作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试使用 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/
我是一名优秀的程序员,十分优秀!