Ios AVCaptureDevice视频缩放因子缩放速率
我正在尝试在AVFoundation中使用AVCaptureDevice实现“收缩缩放”功能:Ios AVCaptureDevice视频缩放因子缩放速率,ios,swift,avfoundation,avcapturedevice,uipinchgesturerecognizer,Ios,Swift,Avfoundation,Avcapturedevice,Uipinchgesturerecognizer,我正在尝试在AVFoundation中使用AVCaptureDevice实现“收缩缩放”功能: @IBAction func pinchGestureDetected(_ gestureRecognizer: UIPinchGestureRecognizer) { switch gestureRecognizer.state { case .began: print ("began") self.currenZoomFactor = self
@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
}
}
@iAction func pinchGestureDetected(\uGestureRecognitor:UIPinchGestureRecognitor){
切换手势识别器.state{
案例.开始:
打印(“开始”)
self.currenZoomFactor=self.videoDevice!.videoZoomFactor
做{
尝试self.videoDevice!.lockForConfiguration()
}将let错误捕获为NSError{
NSLog(“无法为配置锁定设备:%@”,错误)
}
案例。更改:
打印(“更改”)
var zoomValue:CGFloat=((gesturecognizer.scale)-1)+self.currenZoomFactor
如果zoomValue>min(10.00,self.videoDevice!.activeFormat.videoMaxZoomFactor){
zoomValue=min(10.00,self.videoDevice!.activeFormat.videoMaxZoomFactor)
}否则,如果zoomValue<1.00{
zoomValue=1.00
}
self.videoDevice!.videoZoomFactor=sentZoomValue
案例。结束,。取消:
打印(“结束/取消”)
self.videoDevice!.unlockForConfiguration()
违约:
打破
}
}
上面的工作很好。但是,在上述情况下,缩放率与收缩比例成线性关系。这使得缩放因子越高,缩放速度越慢
如何在更高的缩放因子下获得加速缩放率?要获得加速缩放率,我们需要以下一些计算 您可以从
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)
}
}
这仍然很慢。。。我想知道是否有办法进一步优化它。Instagram和本地摄像头解决了这个问题。感觉就像他们在操纵
previewLayer
的UIImageView
。@mabounassif同意你的观点。这个解决方法有点慢,我记得我的灵感来自于一个苹果示例代码库。我将与Instagram团队核实,并在此处标记我的发现。
@IBAction func pinchGestureDetected(_ gestureRecognizer: UIPinchGestureRecognizer) {
zoomto(scale: gestureRecognizer.scale, hasBegunToZoom:(gestureRecognizer.state == .began))
}