我正在使用AVFoundation创建一个视频摄像头UI。包含视图控制器只会在横向右侧方向显示。当通过预览层更改输出方向时,它会正确显示:
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;
}
解决方案:
这个问题可以通过同时采用@Spectravideo328答案中的所有建议,并将预览层添加到ViewControllers视图层上来解决,而不是displayContainer子视图。