Nik*_*een 9 objective-c rotation avfoundation ios avcapturesession
我有一个AVCaptureVideoPreviewLayer实例添加到视图控制器视图层次结构.
- (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;
}
Run Code Online (Sandbox Code Playgroud)
图层框架在-viewDidLayoutSubviews方法中更新.视图控制器方向被锁定UIInterfaceOrientationMaskLandscapeRight.
问题如下:


我尝试将视频图层方向更新为以下(没有结果):
AVCaptureSessionDidStartRunningNotification和UIApplicationDidBecomeActiveNotification通知-viewWillTransitionToSize:withTransitionCoordinator:方法时调用更新-viewWillAppear:该问题似乎与视频层方向本身无关,而是与查看层次结构布局有关.
更新:
正如所建议的那样,我也尝试更新设备方向更改的视频图层方向,但没有帮助.
我还注意到,问题主要发生在应用程序启动后,屏幕首次出现.在同一会话期间的后续屏幕演示中,问题的重现率非常低(类似于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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
678 次 |
| 最近记录: |