2013-03-10 63 views
4

我使用AVFoundation创建视频相机UI。包含的视图控制器只能以LandscapeRight方向显示。当改变通过预览层的输出方向,它会显示正确:改变AVCaptureVideoPreviewLayer方向的问题

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

不过,我意识到AVCaptureVideoPreviewLayer的方向属性,而AVCaptureConnection.videoOrientation被弃用。改为使用时:

self.previewLayer.connection.videoOrientation = AVCaptureVideoOrientationLandscapeRight; 

即使尝试不同方向的选项,预览图层也不会改变方向。我错过了另一个步骤,以推荐的方式做到这一点?

编辑:

下面是出现在视图控制器自动旋转方法:

-(BOOL)shouldAutorotate 
{ 
    return NO; 
} 

-(NSUInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskLandscapeRight; 
} 

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 
{ 
    return UIInterfaceOrientationLandscapeRight; 
} 

SOLUTION:

这是解决双方通过应用所有建议的@ Spectravideo328的答案,通过将预览图层添加到ViewControllers视图图层,而不是displayContainer子视图。

+0

请在下面看到更新的答案。 – Spectravideo328 2013-03-10 21:19:43

回答

1

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

所有CALayers需要他们的帧设置:在你的情况,我只想添加到您的代码:

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

您使用self.view.bounds而不是self.view.frame使这是关键如果设备旋转,则预览层尺寸将旋转(调整)。

希望这会有所帮助。

更新: 这是从supportedInterfaceOrientations帮助说明:

当用户改变设备方向时,系统调用根视图控制器或填充窗口的最上面的呈现视图控制器上此方法。如果视图控制器支持新的方向,则窗口和视图控制器将旋转到新的方向。只有在视图控制器的shouldAutorotate方法返回YES时才调用此方法。

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

+0

感谢您提供关于边界与帧的注释,我没有意识到这种区别。但是,这不能解决问题。无论我使用什么方向值,预览图层都保持不变。 – Chris 2013-03-10 17:36:07

+0

@Chris,我认为还有其他一些地方可能会出现问题。让我们从这个开始吧:你的VC中是否有shouldAutorotateToInterfaceOrientation让VC知道接受哪些方向? – Spectravideo328 2013-03-10 19:36:33

+0

刚刚更新了原来的问题。 – Chris 2013-03-10 20:58:56