Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's the solution: <a href="http://developer.apple.com/library/ios/#documentation/uikit/reference/UIDevice_Class/Reference/UIDevice.html#//apple_ref/occ/instm/UIDevice/endGeneratingDeviceOrientationNotifications" rel="nofollow noreferrer">[UIDevice endGeneratingDeviceOrientationNotifications]</a>.</p> <p>Why was it so hard to find? Two reasons:</p> <ol> <li>The <code>UIDevice</code> 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.</li> <li>The <code>UIImagePickerController</code> switches these notifications on when it is presented.</li> </ol> <p>So, calling this method once did nothing to the image picker. To make sure orientation notifications are off and <em>stay</em> off, you need to switch them off before <em>and</em> after the picker is presented.</p> <p>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.</p> <pre><code>@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]; } </code></pre> <p>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 <a href="https://stackoverflow.com/a/7721814/1318452">this answer to How to Rotate a UIImage 90 degrees?</a></p> <pre><code>//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]; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload