Zooming while capturing video using AVCapture in iOS

强颜欢笑 提交于 2019-12-01 02:01:15

I faced the same problem also, and I have solved it using these two steps:

  1. Add a PinchGestureRecognizer event something like that in your Camera Preview View Controller

    - (IBAction)handlePinchGesture:(UIPinchGestureRecognizer *)gestureRecognizer{
    
    if([gestureRecognizer isMemberOfClass:[UIPinchGestureRecognizer class]])
    {
    
            effectiveScale = beginGestureScale * ((UIPinchGestureRecognizer *)gestureRecognizer).scale;
            if (effectiveScale < 1.0)
                effectiveScale = 1.0;
            CGFloat maxScaleAndCropFactor = [[self.stillImageOutput connectionWithMediaType:AVMediaTypeVideo] videoMaxScaleAndCropFactor];
            if (effectiveScale > maxScaleAndCropFactor)
                effectiveScale = maxScaleAndCropFactor;
            [CATransaction begin];
            [CATransaction setAnimationDuration:.025];
            [self.previewView.layer setAffineTransform:CGAffineTransformMakeScale(effectiveScale, effectiveScale)];
            [CATransaction commit];
    
        if ([[self videoDevice] lockForConfiguration:nil]) {
            [[self videoDevice] setVideoZoomFactor:effectiveScale];
            [[self videoDevice] unlockForConfiguration];
        }}}}
    

** Note that the key method for persisting the zoom level for video device is [device setVideoZoomFactor:]

2- In the IBAction of the Record Button , add this code to capture the video ( recording ) then to save the recorded video in the a certain path with certain name

- (IBAction)recordButtonClicked:(id)sender {

dispatch_async([self sessionQueue], ^{
    if (![[self movieFileOutput] isRecording])
    {
        [self setLockInterfaceRotation:YES];

        if ([[UIDevice currentDevice] isMultitaskingSupported])
        {
            // Setup background task. This is needed because the captureOutput:didFinishRecordingToOutputFileAtURL: callback is not received until the app returns to the foreground unless you request background execution time. This also ensures that there will be time to write the file to the assets library when the app is backgrounded. To conclude this background execution, -endBackgroundTask is called in -recorder:recordingDidFinishToOutputFileURL:error: after the recorded file has been saved.
            [self setBackgroundRecordingID:[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil]];
        }

        // Update the orientation on the movie file output video connection before starting recording.
        // Start recording to a temporary file.
        NSString *outputFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:[@"movie" stringByAppendingPathExtension:@"mov"]];
        [[self movieFileOutput] startRecordingToOutputFileURL:[NSURL fileURLWithPath:outputFilePath] recordingDelegate:self];
    }
    else
    {

        [[self movieFileOutput] stopRecording];

    }
});
}

I hope that helps you

Add UIPinchGestureRecognizer object to your and handle callback like this:

- (void) zoomPinchGestureRecognizerAction: (UIPinchGestureRecognizer *) sender {
    static CGFloat initialVideoZoomFactor = 0;

    if (sender.state == UIGestureRecognizerStateBegan) {

        initialVideoZoomFactor = _captureDevice.videoZoomFactor;

    } else {

        CGFloat scale = MIN(MAX(1, initialVideoZoomFactor * sender.scale), 4);

        [CATransaction begin];
        [CATransaction setAnimationDuration: 0.01];
        _previewLayer.affineTransform = CGAffineTransformMakeScale(scale, scale);
        [CATransaction commit];

        if ([_captureDevice lockForConfiguration: nil] == YES) {
            _captureDevice.videoZoomFactor = scale;
            [_captureDevice unlockForConfiguration];
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!