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