我正在使用Swift,我希望能够在旋转到横向时加载UIViewController,任何人都可以指向正确的方向吗?
我在网上找不到任何东西,文档也有点困惑.
我已经实现了下面的代码来改变AVCaptureVideoSession基于以下内容的方向UIInterfaceOrientation:
- (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 (%ld)",(long)orientation);
return AVCaptureVideoOrientationPortrait;
}
Run Code Online (Sandbox Code Playgroud)
这段代码几乎完美无缺.但是,出现的一个问题是,如果您快速将iOS设备旋转180度,相机将显示为颠倒.
以下是旋转前相机视图的样子:

这是旋转后的样子:

另外,这是我的实现viewDidLayoutSubviews:
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
previewLayer.frame = self.view.bounds;
self.view.translatesAutoresizingMaskIntoConstraints = NO;
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[self.view.layer addSublayer:previewLayer];
if (previewLayer.connection.supportsVideoOrientation) {
previewLayer.connection.videoOrientation = [self interfaceOrientationToVideoOrientation:[UIApplication sharedApplication].statusBarOrientation];
}
}
Run Code Online (Sandbox Code Playgroud)
这个180度旋转发生时有没有人知道为什么摄像机视图会颠倒?
objective-c uiinterfaceorientation ios avcapturesession viewdidlayoutsubviews