AVCaptureDevice Camera Zoom

前端 未结 9 594
没有蜡笔的小新
没有蜡笔的小新 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:07

    based on @Gabriel Cartier 's answer :

    - (void) cameraZoomWithPinchVelocity: (CGFloat)velocity {
        CGFloat pinchVelocityDividerFactor = 40.0f;
        if (velocity < 0) {
            pinchVelocityDividerFactor = 5.; //zoom in
        }
    
        if (_videoInput) {
            if([[_videoInput device] position] == AVCaptureDevicePositionBack) {
                NSError *error = nil;
                if ([[_videoInput device] lockForConfiguration:&error]) {
                    CGFloat desiredZoomFactor = [_videoInput device].videoZoomFactor + atan2f(velocity, pinchVelocityDividerFactor);
                    // Check if desiredZoomFactor fits required range from 1.0 to activeFormat.videoMaxZoomFactor
                    CGFloat maxFactor = MIN(10, [_videoInput device].activeFormat.videoMaxZoomFactor);
                    [_videoInput device].videoZoomFactor = MAX(1.0, MIN(desiredZoomFactor, maxFactor));
                    [[_videoInput device] unlockForConfiguration];
                } else {
                    NSLog(@"cameraZoomWithPinchVelocity error: %@", error);
                }
            }
        }
    }
    

提交回复
热议问题