xcode 在方向锁定的情况下强制横向相机视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13833908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Force camera view in landscape with orientation lock on
提问by Dale Townsend
I am developing an augmented reality game, and I've encountered an issue with the orientation of the camera view when the orientation lock of the device is on.
我正在开发增强现实游戏,当设备的方向锁定打开时,我遇到了相机视图的方向问题。
I am using this code to load the camera view inside of a View:
我正在使用此代码加载视图内的相机视图:
AVCaptureSession *session = [[AVCaptureSession alloc] init];
AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
captureVideoPreviewLayer.frame = self.sessionView.bounds;
[self.sessionView.layer addSublayer:captureVideoPreviewLayer];
CGRect bounds=sessionView.layer.bounds;
captureVideoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
captureVideoPreviewLayer.bounds=bounds;
captureVideoPreviewLayer.orientation = [[UIDevice currentDevice] orientation];
captureVideoPreviewLayer.position=CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
// device.position ;
NSError *error = nil;
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
if ([device hasTorch]) {
([device supportsAVCaptureSessionPreset:AVCaptureSessionPreset1280x720]);
}
else {
([device supportsAVCaptureSessionPreset:AVCaptureSessionPreset640x480]);
}
[session addInput:input];
[session startRunning];
And to keep the orientation of the app in landscape right, I have only that box in the Xcode app Summary selected, with:
为了保持应用程序的横向方向正确,我只选择了 Xcode 应用程序摘要中的那个框,其中:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return ((interfaceOrientation == UIInterfaceOrientationLandscapeRight));
}
When the device has orientation lock on (double click home button, swipe right, tap orientation icon) the camera view is in portrait, with the rest of the game in landscape. Is there any way to fix this? From what I've read it's not possible to turn off orientation lock when the user opens the game.
当设备打开方向锁定(双击主页按钮,向右滑动,点击方向图标)时,相机视图为纵向,游戏的其余部分为横向。有没有什么办法解决这一问题?从我读过的内容来看,当用户打开游戏时无法关闭方向锁定。
回答by Evol Gate
The reason why your Preview layer is not orientating is due the fact that you are using deprecated API and moreover you are not updating the Video orientation at the change of device orientation.
您的预览层未定向的原因是您使用的是已弃用的 API,而且您没有在设备方向更改时更新视频方向。
Remove the deprecated API i.e in your code instead of
captureVideoPreviewLayer.orientation
use videoOrientation property i.e
captureVideoPreviewLayer.connection.videoOrientation
Update the video orientation in shouldAutorotate as follows:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { if(interfaceOrientation == UIInterfaceOrientationLandscapeRight) { captureVideoPreviewLayer.connection.videoOrientation = AVCaptureVideoOrientationLandscapeRight } // and so on for other orientations return ((interfaceOrientation == UIInterfaceOrientationLandscapeRight)); }
删除不推荐使用的 API ie 在您的代码中,而不是
captureVideoPreviewLayer.orientation
使用 videoOrientation 属性,即
captureVideoPreviewLayer.connection.videoOrientation
更新 shouldAutorotate 中的视频方向,如下所示:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { if(interfaceOrientation == UIInterfaceOrientationLandscapeRight) { captureVideoPreviewLayer.connection.videoOrientation = AVCaptureVideoOrientationLandscapeRight } // and so on for other orientations return ((interfaceOrientation == UIInterfaceOrientationLandscapeRight)); }