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.
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.
Remove the deprecated API i.e in your code instead of
captureVideoPreviewLayer.orientation
use videoOrientation property i.e
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));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With