在iOS中使用AVCapture捕获视频时缩放 [英] Zooming while capturing video using AVCapture in iOS

查看:426
本文介绍了在iOS中使用AVCapture捕获视频时缩放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用AVCapture捕获视频并保存。但我需要提供缩放选项,如捏缩放或缩放按钮。视频也应该以与显示相同的方式保存,我的意思是在放大时,应该保存缩放。任何帮助,Link表示赞赏。我设置AVCapture会话的代码是:

I am using AVCapture to capture video and save it. But I need to provide zooming option like pinch to zoom or through a zoom button. Also video should be saved in exactly in same manner in which it is being displayed, I mean when zoomed in, it should be saved zoomed. Any help, Link is appreciated. My code for setting up AVCapture session is:

- (void)setupAVCapture{
session = [[AVCaptureSession alloc] init];
session.automaticallyConfiguresApplicationAudioSession=YES;
[session beginConfiguration];
session.sessionPreset = AVCaptureSessionPresetMedium;
AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
captureVideoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
captureVideoPreviewLayer.frame = self.view.bounds;

[self.view.layer addSublayer:captureVideoPreviewLayer];
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

NSError *error = nil;
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
if (!input) {
    // Handle the error appropriately.
    NSLog(@"ERROR: trying to open camera: %@", error);
}
[session addInput:input];

movieFileOutput = [[AVCaptureMovieFileOutput alloc] init];

[session addOutput:movieFileOutput];
[session commitConfiguration];
[session startRunning];

 }


推荐答案

我遇到了同样的问题,我已经使用以下两个步骤解决了它:

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


  1. 在相机中添加类似于此的PinchGestureRecognizer事件预览视图控制器

  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];
    }}}}


* *请注意,保持视频设备缩放级别的关键方法是[device setVideoZoomFactor:]

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

2-在记录按钮的IBAction中,添加此代码以捕获视频(录制)然后将录制的视频保存在具有特定名称的特定路径中

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];

    }
});
}

我希望能帮到你

这篇关于在iOS中使用AVCapture捕获视频时缩放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆