捏放大照相机

我正在努力缩小相机,但我遇到了两个问题。 首先,它允许用户放大太多的方式和太多的方式,其次,当我拍照时,它不会放大视图。 这里是我的捏function的代码…

func pinch(pinch: UIPinchGestureRecognizer) { if let view = cameraView { view.transform = CGAffineTransformScale(view.transform, pinch.scale, pinch.scale) pinch.scale = 1 } } 

告诉我,如果你需要看到更多的代码。 谢谢!

我已经遇到了与相机实施相同的问题。 要解决这个问题,你需要知道两件事。

  • 最大和最小变焦必须在一个值内,否则会导致相机变焦太大。
  • 与实际图像不保存放大图像一样,这是一个常见的错误,很多在线解决scheme都没有涉及。 这实际上是因为你只是改变视图的缩放而不是实际的AVCaptureDevice的缩放。

要改变这两件事你需要像这样的东西:

 func pinch(pinch: UIPinchGestureRecognizer) { var device: AVCaptureDevice = self.videoDevice var vZoomFactor = ((gestureRecognizer as! UIPinchGestureRecognizer).scale) var error:NSError! do{ try device.lockForConfiguration() defer {device.unlockForConfiguration()} if (vZoomFactor <= device.activeFormat.videoMaxZoomFactor){ device.videoZoomFactor = vZoomFactor }else{ NSLog("Unable to set videoZoom: (max %f, asked %f)", device.activeFormat.videoMaxZoomFactor, vZoomFactor); } }catch error as NSError{ NSLog("Unable to set videoZoom: %@", error.localizedDescription); }catch _{ } } 

正如你所看到的,我使用video设备( videoDevice )的类variables来跟踪我用于视觉组件的捕获设备。 我将缩放限制在一个特定的范围,并改变设备上的缩放属性,而不是视图本身!

Swift 3.0

 let minimumZoom: CGFloat = 1.0 let maximumZoom: CGFloat = 3.0 var lastZoomFactor: CGFloat = 1.0 func pinch(_ pinch: UIPinchGestureRecognizer) { guard let device = videoDeviceInput.device else { return } // Return zoom value between the minimum and maximum zoom values func minMaxZoom(_ factor: CGFloat) -> CGFloat { return min(min(max(factor, minimumZoom), maximumZoom), device.activeFormat.videoMaxZoomFactor) } func update(scale factor: CGFloat) { do { try device.lockForConfiguration() defer { device.unlockForConfiguration() } device.videoZoomFactor = factor } catch { print("\(error.localizedDescription)") } } let newScaleFactor = minMaxZoom(pinch.scale * lastZoomFactor) switch pinch.state { case .began: fallthrough case .changed: update(scale: newScaleFactor) case .ended: lastZoomFactor = minMaxZoom(newScaleFactor) update(scale: lastZoomFactor) default: break } } 
  var device: AVCaptureDevice = self.backCamera var vZoomFactor = sender.scale var error:NSError! do{ try device.lockForConfiguration() defer {device.unlockForConfiguration()} if (vZoomFactor <= device.activeFormat.videoMaxZoomFactor) { let desiredZoomFactor:CGFloat = vZoomFactor + atan2(sender.velocity, 5.0); device.videoZoomFactor = max(1.0, min(desiredZoomFactor, device.activeFormat.videoMaxZoomFactor)); } else { NSLog("Unable to set videoZoom: (max %f, asked %f)", device.activeFormat.videoMaxZoomFactor, vZoomFactor); } } catch error as NSError{ NSLog("Unable to set videoZoom: %@", error.localizedDescription); } catch _{ } 

要扩大Ritvik Upadhyaya的答案,您还需要保存以前的缩放因子来计算新的缩放因子,每当您抬起手指并再次尝试缩放时,您不希望缩放重置为正常。

 // To track the zoom factor var prevZoomFactor: CGFloat = 1 func pinch(pinch: UIPinchGestureRecognizer) { var device: AVCaptureDevice = self.videoDevice // Here we multiply vZoomFactor with the previous zoom factor if it exist. // Else just multiply by 1 var vZoomFactor = pinch.scale * prevZoomFactor // If the pinching has ended, update prevZoomFactor. // Note that we set the limit at 1, because zoom factor cannot be less than 1 or the setting device.videoZoomFactor will crash if sender.state == .ended { prevZoomFactor = zoomFactor >= 1 ? zoomFactor : 1 } do { try device.lockForConfiguration() defer {device.unlockForConfiguration()} if (vZoomFactor <= device.activeFormat.videoMaxZoomFactor) { device.videoZoomFactor = vZoomFactor } else { print("Unable to set videoZoom: (max \(device.activeFormat.videoMaxZoomFactor), asked \(vZoomFactor))") } } catch { print("\(error.localizedDescription)") } } 

如果你需要一个手动的zoomTo(2.0)函数,你可以使用这个

 // Create listener for Pinch to Zoom let pinchRecognizer = UIPinchGestureRecognizer(target: self, action:#selector(FSCameraView.pinchToZoom(_:))) pinchRecognizer.delegate = self self.previewViewContainer.addGestureRecognizer(pinchRecognizer) // set the zoom to a zoomed in mode from start setZoom(CGFloat(2.0) // and the functions func pinchToZoom(sender:UIPinchGestureRecognizer) { var vZoomFactor = ((sender as! UIPinchGestureRecognizer).scale) setZoom(vZoomFactor) } func setZoom(zoomFactor:CGFloat) { var device: AVCaptureDevice = self.device! var error:NSError! do{ try device.lockForConfiguration() defer {device.unlockForConfiguration()} if (zoomFactor <= device.activeFormat.videoMaxZoomFactor) { let desiredZoomFactor:CGFloat = zoomFactor + atan2(sender.velocity, 5.0); device.videoZoomFactor = max(1.0, min(desiredZoomFactor, device.activeFormat.videoMaxZoomFactor)); } else { NSLog("Unable to set videoZoom: (max %f, asked %f)", device.activeFormat.videoMaxZoomFactor, zoomFactor); } } catch error as NSError{ NSLog("Unable to set videoZoom: %@", error.localizedDescription); } catch _{ } } 
    Interesting Posts