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 UIGestureRecognize
Since iOS 7 you can set the zoom directly with the videoZoomFactor property of AVCaptureDevice
.
Tie the scale
property of the UIPinchGestureRecognizer
to thevideoZoomFactor
with a scaling constant. This will let you vary the sensitivity to taste:
-(void) handlePinchToZoomRecognizer:(UIPinchGestureRecognizer*)pinchRecognizer {
const CGFloat pinchZoomScaleFactor = 2.0;
if (pinchRecognizer.state == UIGestureRecognizerStateChanged) {
NSError *error = nil;
if ([videoDevice lockForConfiguration:&error]) {
videoDevice.videoZoomFactor = 1.0 + pinchRecognizer.scale * pinchZoomScaleFactor;
[videoDevice unlockForConfiguration];
} else {
NSLog(@"error: %@", error);
}
}
}
Note that AVCaptureDevice
, along everything else related to AVCaptureSession
, is not thread safe. So you probably don't want to do this from the main queue.