Pinch to enlarge the camera

I'm trying to take a picture to enlarge the camera, but I am having two problems. Firstly, it allows the user to scale and zoom in too much, and secondly, when I take a picture, he does not perceive the enlarged image. Here is my code for the pinch function ...

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

Please tell me if you need more code. Thanks!

+11
ios iphone swift camera zoom
source share
5 answers

I had the same problems with the implementation of the camera. To solve this problem, you need to know about two things.

  • The maximum and minimum scaling must be within the value, otherwise this will lead to a too large increase in the camera image.
  • Like the actual image that does not store the enlarged image, this is a common mistake that many Internet solutions do not apply. This is because you are zooming, not the actual scaling of the AVCaptureDevice .

To change two things, you need something like this:

 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 _{ } } 

As you can see, I use the class variable for the video device ( videoDevice ) to track the capture device that I use for the visual component. I limit the scale to a certain range and change the zoom property on the device, and not on the view itself!

+12
source share

Swift 3.0 || 4.0


1. Define the zoom levels.

 let minimumZoom: CGFloat = 1.0 let maximumZoom: CGFloat = 3.0 var lastZoomFactor: CGFloat = 1.0 


2. Add a Pinch gesture to the CameraView.

 let pinchRecognizer = UIPinchGestureRecognizer(target: self, action:#selector(pinch(_:))) self.viewCamera.addGestureRecognizer(pinchRecognizer) 


3. The zoomin action method with zoomin and zoom out logic

 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 } } 


Thank you Good coding πŸ‘πŸΌ

+30
source share
  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 _{ } 
+1
source share

If you need the manual zoomTo function (2.0), you can use this

 // 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 _{ } } 
+1
source share

To expand Ritvik Upadhyaya ’s answer, you also need to keep the previous scaling factor to calculate a new one, you don’t want the scaling to reset to be normal every time you raise your fingers and try to scale again.

 // 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)") } } 
+1
source share

All Articles