首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >利用AVCapture在iOS中实现视频放大

利用AVCapture在iOS中实现视频放大
EN

Stack Overflow用户
提问于 2014-12-09 07:07:16
回答 2查看 4.2K关注 0票数 1

我正在使用AVCapture捕捉和保存视频。但我需要提供缩放选项,如捏,以放大或通过缩放按钮。另外,视频应该以与显示完全相同的方式保存,我的意思是当放大时,它应该被放大保存。如有任何帮助,林克将不胜感激。我设置AVCapture会话的代码是:

代码语言:javascript
复制
- (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];

 }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-03-29 19:56:00

我也面临着同样的问题,我用以下两个步骤解决了这个问题:

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

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

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

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

代码语言:javascript
复制
- (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];

    }
});
}

我希望这对你有帮助

票数 4
EN

Stack Overflow用户

发布于 2016-02-17 11:08:19

UIPinchGestureRecognizer对象添加到您的并处理回调中,如下所示:

代码语言:javascript
复制
- (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];
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27373243

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档