Forced camera view in landscape with orientation lock

I'm developing an augmented reality game, and I ran into the problem of orienting the orientation of the camera when the orientation lock of the device is turned on.

I use this code to load the camera view inside the 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 preserve the orientation of the application in landscape mode, I have only this field in the selected Xcode Summary application, with:

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

When the device has an orientation lock (double-click the Home button, swipe right, tap the orientation icon), the camera will be viewed in the portrait, and the rest of the game will be in the landscape. Is there any way to fix this? From what I read, it is not possible to disable orientation lock when a user opens a game.

+4
source share
1 answer

The reason your preview level is not oriented is because you are using an outdated API and, in addition, you are not updating the video orientation when you change the orientation of the device.

  • Remove obsolete API in code instead

     captureVideoPreviewLayer.orientation 

    use the videoOrientation ie property

     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)); } 
+15
source

All Articles