【问题标题】:Zooming while capturing video using AVCapture in iOS在 iOS 中使用 AVCapture 捕捉视频时进行缩放
【发布时间】:2015-02-07 00:12:27
【问题描述】:

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

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

 }

【问题讨论】:

    标签: ios7 camera zooming video-capture avcapturesession


    【解决方案1】:

    我也遇到了同样的问题,我已经用这两个步骤解决了:

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

      - (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:]

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

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

    希望对你有帮助

    【讨论】:

    • 嘿@ahmedSaad,您能否添加更多详细信息。像 beginGestureScale 和其他?
    【解决方案2】:

    UIPinchGestureRecognizer 对象添加到您的并像这样处理回调:

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

    【讨论】:

    • 不应直接使用'4'作为最大比例,而应使用此代码:CGFloat maxScale = captureDevice.activeFormat.videoMaxZoomFactor;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-16
    • 2019-04-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多