更改AVCaptureVideoPreviewLayer方向的问题

时间:2013-03-10 00:30:49

标签: ios6 avfoundation calayer avcapturesession autorotate

我正在使用AVFoundation创建摄像机UI。包含的viewcontroller仅以LandscapeRight方向显示。通过预览图层更改输出方向时,它会正确显示:

self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession];
self.previewLayer.orientation = AVCaptureVideoOrientationLandscapeRight;
[self.displayContainer.layer addSublayer:self.previewLayer];

但是,我意识到AVCaptureVideoPreviewLayer的orientation属性已被弃用,转而支持AVCaptureConnection.videoOrientation。当使用它时:

    self.previewLayer.connection.videoOrientation = AVCaptureVideoOrientationLandscapeRight;

预览图层似乎根本不会改变方向,即使在尝试不同的方向选项时也是如此。为了以推荐的方式做到这一点,我还缺少另一个步骤吗?

编辑:

以下是视图控制器中的自动旋转方法:

-(BOOL)shouldAutorotate
{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeRight;
}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight;
}

解决方案:

通过应用@ Spectravideo328的答案中的所有建议,并将预览图层添加到ViewControllers视图图层,而不是displayContainer子视图,可以解决这个问题。

1 个答案:

答案 0 :(得分:1)

您的问题不是AvCaptureVideoPreviewLayer问题,而是一般的CALayer问题(其中previewLayer是其子类)。

所有CALayers都需要设置框架:在您的情况下,我只会添加到您的代码中:

self.previewlayer.frame=self.view.bounds.

使用self.view.bounds而非self.view.frame至关重要,以便在旋转设备时旋转(调整)预览层尺寸。

希望这有帮助。

更新: 这来自supportedInterfaceOrientations帮助说明:

  

当用户更改设备方向时,系统会在根视图控制器或填充窗口的最顶层显示的视图控制器上调用此方法。如果视图控制器支持新方向,则窗口和视图控制器将旋转到新方向。仅当视图控制器的shouldAutorotate方法返回YES时才调用此方法。

更新您的shouldAutorotate以始终返回YES。

相关问题