Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable rotation in UIImagePicker

Hello I need to disable the rotation of the UIImagePicker.
If the user takes picture when the iPhone is on "landscape" the buttons will rotate and the picture taken will also be rotated. I want to disable that option so the accelerometer will not work and it will always take picture like in portrait mode.
How can I do that?
Thanks, Matan Radomski.

like image 331
user1321887 Avatar asked Apr 09 '12 12:04

user1321887


1 Answers

Here's the solution: [UIDevice endGeneratingDeviceOrientationNotifications].

Why was it so hard to find? Two reasons:

  1. The UIDevice keeps count of how many times orientation notifications are switched on or off. If it has been switched on more times than it's been switched off, notifications will still be issued.
  2. The UIImagePickerController switches these notifications on when it is presented.

So, calling this method once did nothing to the image picker. To make sure orientation notifications are off and stay off, you need to switch them off before and after the picker is presented.

This does not affect iOS or other apps. It doesn't even fully affect your own app: as with the other method I suggested, the camera buttons continue responding to orientation changes, and the photos taken are also orientation aware. This is curious, because the device orientation hardware is supposed to be switched off if it isn't needed.

@try 
{
    // Create a UIImagePicker in camera mode.
    UIImagePickerController *picker = [[[UIImagePickerController alloc] init] autorelease]; 
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;  
    picker.delegate = self; 

    // Prevent the image picker learning about orientation changes by preventing the device from reporting them.
    UIDevice *currentDevice = [UIDevice currentDevice];

    // The device keeps count of orientation requests.  If the count is more than one, it continues detecting them and sending notifications.  So, switch them off repeatedly until the count reaches zero and they are genuinely off.
    // If orientation notifications are on when the view is presented, it may slide on in landscape mode even if the app is entirely portrait.
    // If other parts of the app require orientation notifications, the number "end" messages sent should be counted.  An equal number of "begin" messages should be sent after the image picker ends.
    while ([currentDevice isGeneratingDeviceOrientationNotifications])
        [currentDevice endGeneratingDeviceOrientationNotifications];

    // Display the camera.
    [self presentModalViewController:picker animated:YES];

    // The UIImagePickerController switches on notifications AGAIN when it is presented, so switch them off again.
    while ([currentDevice isGeneratingDeviceOrientationNotifications])
        [currentDevice endGeneratingDeviceOrientationNotifications];
}
@catch (NSException *exception) 
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Camera" message:@"Camera is not available" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
    [alert show];
    [alert release];
}

As noted above, the pictures taken may still be in the wrong orientation. If you want them oriented consistently, check their aspect ratio and rotate accordingly. I recommend this answer to How to Rotate a UIImage 90 degrees?

//assume that the image is loaded in landscape mode from disk
UIImage * landscapeImage = [UIImage imageNamed: imgname];
UIImage * portraitImage = [[UIImage alloc] initWithCGImage: landscapeImage.CGImage scale:1.0 orientation: UIImageOrientationLeft] autorelease];
like image 124
Cowirrie Avatar answered Sep 30 '22 16:09

Cowirrie