I have a simple AVCaptureSession running to get a camera feed in my app and take photos. How can I implement the 'pinch to zoom' functionality using a UIGestureRecognizer
for the camera?
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- Could I create “Call” button in HTML 5 IPhone appl
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- xcode 4 garbage collection removed?
- Unable to process app at this time due to a genera
- Swift - hide pickerView after value selected
In swift version, you can zoom in/out by simply passing scaled number on videoZoomFactor. Following code in UIPinchGestureRecognizer handler will solve the issue.
In here, pivotPinchScale is a CGFloat property that declared in your controller somewhere.
You may also refer to following project to see how camera works with UIPinchGestureRecognizer. https://github.com/DragonCherry/CameraPreviewController
I am using iOS SDK 8.3 and the AVfoundation framework and for me using the following method worked for :
For saving the picture with the same scale I used the following method:
The code bellow is for getting the image in the scale
I started from the @Gabriel Cartier's solution (thanks). In my code I've preferred to use the smoother rampToVideoZoomFactor and a simpler way to compute the device's scale factor.
Since iOS 7 you can set the zoom directly with the
videoZoomFactor
property ofAVCaptureDevice
.Tie the
scale
property of theUIPinchGestureRecognizer
to thevideoZoomFactor
with a scaling constant. This will let you vary the sensitivity to taste:Note that
AVCaptureDevice
, along everything else related toAVCaptureSession
, is not thread safe. So you probably don't want to do this from the main queue.Many have tried to do this by setting the transform property on the layer to
CGAffineTransformMakeScale(gesture.scale.x, gesture.scale.y);
See here for a full fledged implementation of pinch-to-zoom.The accepted answer is actually outdated and I'm not sure it will actually take the photo of the zoomed in image. There is a method to zoom in like bcattle answer says. The problem of his answer is that it does not take in charge the fact that the user can zoom in and then restart from that zoom position. His solution will create some kind of jumps that are not really elegant.
The easiest and most elegant way of doing this is to use the velocity of the pinch gesture.
I found that adding the arctan function to the velocity will ease the zoom in zoom out effect a bit. It is not exactly perfect but the effect is good enough for the needs. There could probably be another function to ease the zoom out when it almost reaches 1.
NOTE: Also, the scale of a pinch gesture goes from 0 to infinite with 0 to 1 being pinching in (zoom out) and 1 to infinite being pinching out (zoom in). To get a good zoom in zoom out effect with this you'd need to have a math equation. Velocity is actually from -infinite to infinite with 0 being the starting point.
EDIT: Fixed crash on range exception. Thanks to @garafajon!