我有一个 AVCaptureVideoPreviewLayer
实例添加到 View Controller View 层次结构中。
- (void) loadView {
...
self.previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:nil];
self.previewLayer.frame = self.view.bounds;
self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[self.view.layer addSublayer: _previewLayer];
// adding other UI elements
...
}
...
- (void) _setupPreviewLayerWithSession: (AVCaptureSession*) captureSession
{
self.previewLayer.session = self.captureManager.captureSession;
self.previewLayer.connection.videoOrientation = AVCaptureVideoOrientationLandscapeRight;
}
层框架在 -viewDidLayoutSubviews
方法中更新。 View Controller 方向锁定为 UIInterfaceOrientationMaskLandscapeRight
。
问题如下:
- 设备横向放置
- View Controller 以模态方式呈现 - 视频层正确显示。
- 然后设备被锁定,并且在锁定期间设备旋转到纵向。
- 然后解锁设备同时仍处于纵向并且视频层旋转 90 度显示几秒钟。但是,视频层的帧是正确的。所有其他 UI 元素都正确显示。几秒钟后,图层会捕捉到正确的方向。 请在下面找到层和 UI 元素的边界
我试过如下更新视频层方向(没有结果):
- 订阅
AVCaptureSessionDidStartRunningNotification
和UIApplicationDidBecomeActiveNotification
通知 - 调用
-viewWillTransitionToSize:withTransitionCoordinator:
方法时调用更新 - 在
-viewWillAppear 上:
这个问题似乎与视频层方向本身无关,而更多地与 View 层次结构布局有关。
更新:
按照建议,我还尝试在设备方向更改时更新视频层方向,但没有帮助。
我还注意到这个问题主要发生在应用程序启动并首次显示屏幕之后。在同一 session 的后续屏幕演示中,问题的重现率非常低(大约 1/20)。
最佳答案
试试这段代码:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
-(void)orientationChanged:(NSNotification *)notif {
[_videoPreviewLayer setFrame:_viewPreview.layer.bounds];
if (_videoPreviewLayer.connection.supportsVideoOrientation) {
_videoPreviewLayer.connection.videoOrientation = [self interfaceOrientationToVideoOrientation:[UIApplication sharedApplication].statusBarOrientation];
}
}
- (AVCaptureVideoOrientation)interfaceOrientationToVideoOrientation:(UIInterfaceOrientation)orientation {
switch (orientation) {
case UIInterfaceOrientationPortrait:
return AVCaptureVideoOrientationPortrait;
case UIInterfaceOrientationPortraitUpsideDown:
return AVCaptureVideoOrientationPortraitUpsideDown;
case UIInterfaceOrientationLandscapeLeft:
return AVCaptureVideoOrientationLandscapeLeft;
case UIInterfaceOrientationLandscapeRight:
return AVCaptureVideoOrientationLandscapeRight;
default:
break;
}
// NSLog(@"Warning - Didn't recognise interface orientation (%d)",orientation);
return AVCaptureVideoOrientationPortrait;
}
关于ios - 禁用 UI 旋转时 AVCaptureVideoPreviewLayer 方向错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43658923/