2016-02-06 50 views
2

我有一个自定义的照片/视频摄像头(想想Snapchat)与缩放识别器放大/缩小。下面是一帆风顺的事情基于一些代码,我在网上找到:如何在自定义相机中实现“捏缩放”

  • 在一定程度上放大工作正常
  • 捕捉的图像捕捉放大的图像

这里的什么错误,我需要帮助:

  • 缩小导致崩溃
  • 虽然在作品放大,似乎重置,如果我放大变焦,停止触摸屏幕,然后尝试再次放大。
  • 捕获视频复位变焦

这是我的捏放码,应该怎样改?

for input in self.captureSession.inputs { 
      // check that the input is a camera and not the audio 
      if input.device == self.frontCameraDevice || input.device == self.backCameraDevice { 

       if pinch.state == UIGestureRecognizerState.Changed { 

        let device: AVCaptureDevice = input.device 
        let vZoomFactor = pinch.scale 
        do{ 
         try device.lockForConfiguration() 
         if vZoomFactor <= device.activeFormat.videoMaxZoomFactor { 
          device.videoZoomFactor = vZoomFactor 
          device.unlockForConfiguration() 
         } 
        }catch _{ 
        } 
       } 

      } 
     } 

回答

0

当您缩小pinch.scale值将变得小于1.0,那么应用程序将崩溃。

方法-1

//just change this line 
if pinch.scale > 1.0 { 
    device.videoZoomFactor = vZoomFactor 
} else { 
    device.videoZoomFactor = 1.0 + vZoomFactor 
} 

方法 - 2

可以实现通过变换avcapturesession预览层挤捏缩放。

yourPreviewLayer.affineTransForm = CGAffineTransformMakeScale(1.0 + pinch.scale.x, 1.0 +pinch.scale.y) 

当视频捕获方法调用预览层变换为标识。所以它会重置缩放。

yourPreviewLayer.affineTransForm = CGAffineTransformIdentity 
3

您必须根据以前的值设置videoZoomFactor。

do { 
    try device.lockForConfiguration() 
    switch gesture.state { 
    case .began: 
     self.pivotPinchScale = device.videoZoomFactor 
    case .changed: 
     var factor = self.pivotPinchScale * gesture.scale 
     factor = max(1, min(factor, device.activeFormat.videoMaxZoomFactor)) 
     device.videoZoomFactor = factor 
    default: 
     break 
    } 
    device.unlockForConfiguration() 
} catch { 
    // handle exception 
} 

你应该为了开始放大,从当前的缩放状态/,self.pivotPinchScale在上面的例子中是关键保存原比例因子。 我希望你能从下面的例子中得到一些提示。

https://github.com/DragonCherry/CameraPreviewController