强迫在风景的照相机视图与方向锁

Dal*_*end 4 iphone xcode objective-c ipad ios

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

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

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];
Run Code Online (Sandbox Code Playgroud)

为了保持应用程序的横向正确,我只选择了Xcode应用程序摘要中的那个框,其中:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return ((interfaceOrientation == UIInterfaceOrientationLandscapeRight));
}
Run Code Online (Sandbox Code Playgroud)

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

Evo*_*ate 16

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

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

    captureVideoPreviewLayer.orientation
    
    Run Code Online (Sandbox Code Playgroud)

    使用videoOrientation属性即

    captureVideoPreviewLayer.connection.videoOrientation 
    
    Run Code Online (Sandbox Code Playgroud)
  2. 更新shouldAutorotate中的视频方向,如下所示:

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
    
        if(interfaceOrientation == UIInterfaceOrientationLandscapeRight)
        {
           captureVideoPreviewLayer.connection.videoOrientation = AVCaptureVideoOrientationLandscapeRight
        }
    
          // and so on for other orientations
    
        return ((interfaceOrientation == UIInterfaceOrientationLandscapeRight));
    }
    
    Run Code Online (Sandbox Code Playgroud)

  • 一直在寻找解决方案.谢谢! (2认同)
  • +1为captureVideoPreviewLayer.connection.videoOrientation.谢谢. (2认同)