【问题标题】:Landscape in AvCam iOS 6AvCam iOS 6 中的横向
【发布时间】:2012-10-19 01:35:59
【问题描述】:

我是 iOS 新手,正在尝试使用 AvCam 创建自定义相机。我无法获得横向预览 - 它会将视图顺时针旋转 90 度并在半屏上显示。

我收到了这条消息——

警告:-[ setOrientation:] 已弃用。

请使用 AVCaptureConnection 的 -setVideoOrientation:

AVCaptureConnection 已经设置了方向,所以我不知道我应该怎么做。

我知道以前版本的 iOS (4,5) 曾多次问过这个问题,但这些技术/代码都不适用于我 (iOS 6)。

原始代码(Apple 未做任何更改)

if ([self captureManager] == nil) {
    AVCamCaptureManager *manager = [[AVCamCaptureManager alloc] init];
    [self setCaptureManager:manager];
    [manager release];

    [[self captureManager] setDelegate:self];

    if ([[self captureManager] setupSession]) {
        // Create video preview layer and add it to the UI
        AVCaptureVideoPreviewLayer *newCaptureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:[[self captureManager] session]];


        UIView *view = [self videoPreviewView];
        CALayer *viewLayer = [view layer];
        [viewLayer setMasksToBounds:YES];

        CGRect bounds = [view bounds];
        [newCaptureVideoPreviewLayer setFrame:bounds];

        if ([newCaptureVideoPreviewLayer isOrientationSupported]) {
            [newCaptureVideoPreviewLayer setOrientation:AVCaptureVideoOrientationPortrait];
        }



        [newCaptureVideoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];

        [viewLayer insertSublayer:newCaptureVideoPreviewLayer below:[[viewLayer sublayers] objectAtIndex:0]];

        [self setCaptureVideoPreviewLayer:newCaptureVideoPreviewLayer];

AVCaptureConnection 块:

-(void)startRecordingWithOrientation:(AVCaptureVideoOrientation)videoOrientation; {
AVCaptureConnection *videoConnection = [AVCamUtilities connectionWithMediaType:AVMediaTypeVideo fromConnections:[[self movieFileOutput] connections]];
if ([videoConnection isVideoOrientationSupported])
    [videoConnection setVideoOrientation:videoOrientation];

[[self movieFileOutput] startRecordingToOutputFileURL:[self outputFileURL] recordingDelegate:self];

}

【问题讨论】:

    标签: iphone xcode ios6 landscape avcam


    【解决方案1】:

    上次我也偶然发现了这个问题。我通过做两件事解决了这个问题

    1. 获得正确的方向
      替换

      if ([newCaptureVideoPreviewLayer isOrientationSupported]) {
          [newCaptureVideoPreviewLayer setOrientation:AVCaptureVideoOrientationPortrait];
      }  
      

      与

      if ([newCaptureVideoPreviewLayer.connection isVideoOrientationSupported]) {  
          [newCaptureVideoPreviewLayer.connection setVideoOrientation:[UIDevice currentDevice].orientation];
      }
      
    2. 通过触发在初始化期间强制更新视频方向以捕获横向模式下的视频输出 - (void)deviceOrientationDidChange 内手动AVCaptureManager.m

      我添加到:

      - (BOOL) setupSession
      {
          BOOL success = NO;
      
          ...
      
          AVCamRecorder *newRecorder = [[AVCamRecorder alloc] initWithSession:[self session] outputFileURL:self.lastOutputfileURL];
          [newRecorder setDelegate:self];
      
          [self performSelector:@selector(deviceOrientationDidChange)];
      
          ...
      
          return success;
      }
      

    【讨论】:

      【解决方案2】:

      所以,这里有一个解决方法,但我确信一定有比这更好的解决方案。我从这个问题中得到了部分代码:

      iPhone App - Show AVFoundation video on landscape mode

      但必须为每个方向调整框架以使其在 iOS6 上工作(它仍然显示警告):


      -(void)willAnimateRotationToInterfaceOrientation: (UIInterfaceOrientation)到InterfaceOrientation 持续时间:(NSTimeInterval)持续时间{

      [CATransaction begin];
      if (toInterfaceOrientation==UIInterfaceOrientationLandscapeLeft){
          captureVideoPreviewLayer.orientation = UIInterfaceOrientationLandscapeLeft;
          captureVideoPreviewLayer.frame = CGRectMake(0, 0, 480, 320);
      
      } else if (toInterfaceOrientation==UIInterfaceOrientationPortrait){
          captureVideoPreviewLayer.orientation = UIInterfaceOrientationPortrait;
          captureVideoPreviewLayer.frame = CGRectMake(0, 0, 320, 480);
      
      } else if (toInterfaceOrientation==UIInterfaceOrientationLandscapeRight){
          captureVideoPreviewLayer.orientation = UIInterfaceOrientationLandscapeRight;
          captureVideoPreviewLayer.frame = CGRectMake(0, 0, 480, 320);
      
      }
      [CATransaction commit];
      [super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
      

      }

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多