加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

iphone – 强制相机视图在风景与方向锁定

发布时间:2020-12-15 01:48:44 所属栏目:百科 来源:网络整理
导读:我正在开发一种增强现实游戏,当设备的方向锁定打开时,我遇到了摄像机视图方向的问题. 我正在使用此代码加载视图内的摄像机视图: AVCaptureSession *session = [[AVCaptureSession alloc] init];AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[A
我正在开发一种增强现实游戏,当设备的方向锁定打开时,我遇到了摄像机视图方向的问题.

我正在使用此代码加载视图内的摄像机视图:

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应用程序摘要中的那个框,其中包含:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return ((interfaceOrientation == UIInterfaceOrientationLandscapeRight));
}

当设备开启方向锁定(双击主页按钮,向右滑动,点击方向图标)时,摄像机视图将显示为纵向,其余部分处于横向状态.有没有什么办法解决这一问题?根据我的阅读,当用户打开游戏时,无法关闭方向锁定.

解决方法

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

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读