AVCaptureDevice Camera Zoom

前端 未结 9 596
没有蜡笔的小新
没有蜡笔的小新 2021-01-31 19:51

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

9条回答
  •  醉话见心
    2021-01-31 20:10

    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.

提交回复
热议问题