Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: camera orientation

I want to capture an image with the camera using AVCaptureSession.

It works ok, I start the camera and I can get the output. However, I have some problems with video orientation when I rotate the device.

First, I want to support landscape left and right orientations and may be portrait modes too later.

I implement:

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

When I rotate the device, it rotates the app from landscape left to landscape right or vice versa, but I only see the camera correctly when I'm on landscape left. When the app is on landscape right, the video is rotated by 180 degrees.

Thank you very much.

Update:

I've tried Spectravideo328 answer but I have an error when I try to rotate the device and the app crash. This is the error:

[AVCaptureVideoPreviewLayer connection]: unrecognized selector sent to instance 0xf678210

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[AVCaptureVideoPreviewLayer connection]: unrecognized selector sent to instance 0xf678210'

The error occurs in this line:

AVCaptureConnection *previewLayerConnection=self.previewLayer.connection;

I put it inside shouldAutorotateToInterfaceOrientation method. Do you know what could be the reason of this error?

Thanks

like image 424
A.Vila Avatar asked Feb 11 '13 12:02

A.Vila


People also ask

How do I change the camera orientation on my iPhone?

Swipe down from the top-right corner of your screen to open Control Center. Tap the Portrait Orientation Lock button to make sure that it's off. Turn your iPhone sideways.

How do I stop my iPhone camera from flipping photos?

Swipe down to open the Control Center. Tap Portrait Orientation Lock, then flip your phone into a landscape orientation. Your screen will flip after you do this. Remembering to do this each time will ensure that your photo orientation is always correct.


1 Answers

The default camera orientation is oddly enough UIInterfaceOrientationLeft.

The camera orientation does not change with the rotation of the device. They are separate. You have to adjust the camera orientation manually:

Put the following in a method that you pass toInterfaceOrientation to (maybe you call it from shouldAutorotateToInterfaceOrientation above so that the device rotates and the camera rotates):

You have to get the preview Layer connection first

AVCaptureConnection *previewLayerConnection=self.previewLayer.connection;

if ([previewLayerConnection isVideoOrientationSupported])
{
    switch (toInterfaceOrientation)
    {
        case UIInterfaceOrientationPortrait:
            [previewLayerConnection setVideoOrientation:AVCaptureVideoOrientationPortrait];
            break;
        case UIInterfaceOrientationLandscapeRight:
            [previewLayerConnection setVideoOrientation:AVCaptureVideoOrientationLandscapeRight]; //home button on right. Refer to .h not doc
            break;
        case UIInterfaceOrientationLandscapeLeft:
            [previewLayerConnection setVideoOrientation:AVCaptureVideoOrientationLandscapeLeft]; //home button on left. Refer to .h not doc
            break;
        default:
            [previewLayerConnection setVideoOrientation:AVCaptureVideoOrientationPortrait]; //for portrait upside down. Refer to .h not doc
            break;
    }
}
like image 139
Khaled Barazi Avatar answered Sep 27 '22 22:09

Khaled Barazi