强迫在风景的照相机视图与方向锁 [英] Force camera view in landscape with orientation lock on

查看:137
本文介绍了强迫在风景的照相机视图与方向锁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一种增强现实游戏,当设备的方向锁定打开时,我遇到了摄像机视图方向的问题。

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];

为了确保应用程序的方向正确,我在Xcode应用程序中只有那个方框选择摘要,包括:

And to keep the orientation of the app in landscape right, I have only that box in the Xcode app Summary selected, with:

- (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.

推荐答案

你的原因预览图层没有定位是因为您使用了已弃用的API,而且您没有在更改设备方向时更新视频方向。

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.


  1. 删除已弃用的API,即代码中的

  1. Remove the deprecated API i.e in your code instead of

captureVideoPreviewLayer.orientation

使用videoOrientation属性即

use videoOrientation property i.e

captureVideoPreviewLayer.connection.videoOrientation 


  • 更新shouldAutorotate中的视频方向,如下所示:

  • 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));
    }
    


  • 这篇关于强迫在风景的照相机视图与方向锁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆