我正在尝试使用AVCaptureVideoPreviewLayer让相机以横向模式显示,但无论我做什么,相机在横向模式下始终只显示一半的屏幕。
我拥有它所以我的应用程序仅支持Landscape Right,当应用程序启动时,视图方向显然是Landscape Right,但是当我将视频预览图层的框架设置为视图的边界时,就好像它是仍处于纵向模式。如果我记录视图的宽度和高度,它将打印320x568而不是568x320。
我搜索并查看了与我正在尝试做的相关的每个主题和指南,并且没有找到适用于其中任何一个的解决方案。
这是我的代码(在viewDidLoad中):
// Create video preview layer and add it to the UI
self.videoLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureManager.captureSession];
[[self.view layer] setMasksToBounds:YES];
self.videoLayer.frame = self.view.bounds;
if(self.videoLayer.connection.supportsVideoOrientation)
{
self.videoLayer.connection.videoOrientation = AVCaptureVideoOrientationLandscapeRight;
}
[self.videoLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
[self.view.layer addSublayer:self.videoLayer];
[self.captureManager.captureSession startRunning];
以上代码在我的设备上生成: http://i.imgur.com/Yz1Bgow.jpg
非常感谢任何帮助。
所以我提出了各种各样的解决方案。我不知道是否有办法解决这个问题,但这是我能想到的唯一方法,以便在相机出现之前没有延迟。我基本上只是用视图的翻转宽度和高度制作了一个新的CGRect。
这是固定代码:
// Create video preview layer and add it to the UI
self.videoLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureManager.captureSession];
[self.view.layer setMasksToBounds:YES];
CGSize landscapeSize;
landscapeSize.width = self.view.bounds.size.height;
landscapeSize.height = self.view.bounds.size.width;
CGRect landscapeRect;
landscapeRect.size = landscapeSize;
landscapeRect.origin = self.view.bounds.origin;
self.videoLayer.frame = landscapeRect;
//NSLog(@"%f, %f", self.view.frame.size.width, self.view.frame.size.height);
if(self.videoLayer.connection.supportsVideoOrientation)
{
self.videoLayer.connection.videoOrientation = AVCaptureVideoOrientationLandscapeRight;
}
[self.videoLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
[self.view.layer addSublayer:self.videoLayer];
[self.captureManager.captureSession startRunning];