2

我正在使用最新的 SDK 开发一个 iOS 应用程序。

我正在开发一个仅支持 LandscapeRight 方向的相机应用程序。

在 MainViewController我添加了一个UIViewfor AVCaptureVideoPreviewLayer。换句话说, mainViewController我有主视图和另一个视图,称为videoPreviewView,for AVCaptureVideoPreviewLayer

我设置了AVCaptureVideoPreviewLayer方法viewWillAppear:

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self setUpVideo];
}

- (void)setUpVideo
{
    NSLog(@"Set up video");
    if (DataExchanger.cameraManager != nil)
    {
        UIView *view = [self videoPreviewView];
        CALayer *viewLayer = [view layer];
        [viewLayer setMasksToBounds:YES];

        AVCaptureVideoPreviewLayer *newCaptureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:DataExchanger.cameraManager.captureSession];
        CGRect bounds = [view bounds];
        NSLog(@"BOUNDS: %@", NSStringFromCGRect(bounds));

        [newCaptureVideoPreviewLayer setFrame:bounds];
        [newCaptureVideoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];

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

我得到这个日志:

BOUNDS: {{0, 0}, {320, 480}}

但是UIInterfaceOrientationUIInterfaceOrientationLandscapeRight在那个方向上,我会得到这些值:

BOUNDS: {{0, 0}, {480, 320}}

另一个问题是我看到视频旋转了 -90 度

我找到了这个stackoverflow question,但- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation方法从未被触发。

发生了什么?我该如何解决这个问题?

4

1 回答 1

-1
  1. 默认的 AVCaptureVideoPreviewLayer 对象方向是横向的!
  2. AVCaptureVideoPreviewLayer 的方向不会随着设备的方向而改变。您必须手动更改它。
  3. 要手动更改,需要调用预览层的连接并旋转:

iOS6:

 AVCaptureConnection *previewLayerConnection=self.newCaptureVideoPreviewLayer.connection;
 if ([previewLayerConnection isVideoOrientationSupported])
   [previewLayerConnection setVideoOrientation:AVCaptureVideoOrientationLandscapeRight];

< iOS6:

if ([self.newCaptureVideoPreviewLayer isOrientationSupported])   
[self.newCaptureVideoPreviewLayer setOrientation:AVCaptureVideoOrientationLandscapeRight];
于 2013-03-15T12:24:41.290 回答