4

拡張現実ゲームを開発していますが、デバイスの方向ロックがオンになっていると、カメラ ビューの方向に問題が発生しました。

このコードを使用して、ビュー内にカメラ ビューをロードしています。

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

デバイスの方向ロックがオンになっている場合 (ホーム ボタンをダブルクリックし、右にスワイプし、方向アイコンをタップ)、カメラ ビューは縦向きになり、ゲームの残りの部分は横向きになります。これを修正する方法はありますか?私が読んだことから、ユーザーがゲームを開いたときに方向ロックをオフにすることはできません。

4

1 に答える 1

16

プレビュー レイヤーが向きを変えていない理由は、非推奨の API を使用しており、さらにデバイスの向きの変更時にビデオの向きを更新していないためです。

  1. 非推奨の API を削除します。つまり、代わりにコードで

    captureVideoPreviewLayer.orientation
    

    videoOrientation プロパティを使用します。

    captureVideoPreviewLayer.connection.videoOrientation 
    
  2. 次のように shouldAutorotate でビデオの向きを更新します。

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
    
        if(interfaceOrientation == UIInterfaceOrientationLandscapeRight)
        {
           captureVideoPreviewLayer.connection.videoOrientation = AVCaptureVideoOrientationLandscapeRight
        }
    
          // and so on for other orientations
    
        return ((interfaceOrientation == UIInterfaceOrientationLandscapeRight));
    }
    
于 2012-12-12T07:19:58.823 に答える