Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force camera view in landscape with orientation lock on

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

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.

like image 362
Dale Townsend Avatar asked Dec 04 '22 13:12

Dale Townsend


1 Answers

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. Remove the deprecated API i.e in your code instead of

    captureVideoPreviewLayer.orientation
    

    use videoOrientation property i.e

    captureVideoPreviewLayer.connection.videoOrientation 
    
  2. 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));
    }
    
like image 168
Evol Gate Avatar answered Jan 15 '23 21:01

Evol Gate