UIImagePickerController in Landscape

32,032

Solution 1

I haven't checked whether this is illegal, but it worked for me. If you want the UIImagePickerController to start(and stay) in Landscape orientation code:

//Initialize picker

UIImagePickerController * picker = [[UIImagePickerController alloc] init];
   picker.delegate = self;


//set Device to Landscape. This will give you a warning. I ignored it.
//warning: 'UIDevice' may not respond to '-setOrientation:'


[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];

//Set Notifications so that when user rotates phone, the orientation is reset to landscape.
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

//Refer to the method didRotate:   
[[NSNotificationCenter defaultCenter] addObserver:self
              selector:@selector(didRotate:)
               name:@"UIDeviceOrientationDidChangeNotification" object:nil];

//Set the picker source as the camera   
picker.sourceType = UIImagePickerControllerSourceTypeCamera;

//Bring in the picker view   
[self presentModalViewController:picker animated:YES];

The method didRotate:

- (void) didRotate:(NSNotification *)notification

{
      //Maintain the camera in Landscape orientation
 [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];

}

Solution 2

No need to subclass; simply override the modalPresentationStyle property.

    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.modalPresentationStyle = UIModalPresentationFormSheet;
    [viewController presentViewController:picker animated:YES completion:NULL];

Solution 3

If you just need to get rid of the warning try

@interface UIDevice ()
    -(void)setOrientation:(UIDeviceOrientation)orientation;
@end

Solution 4

I've developed a UIImagePicker class in landscape mode. Works great for applications I've developed: hope it works for you too:

GitHub: https://github.com/imaginaryunit/iOSLandscapeThreadedImagePicker.git

Solution 5

Subclass UIImagePickerController and override modalPresentationStyle as follows:

- (UIModalPresentationStyle)modalPresentationStyle
{
    if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
    {
        return UIModalPresentationFormSheet;
    }

    return [super modalPresentationStyle];
}

The image-picker is a form-sheet now and no longer in fullscreen-mode, but it looks good in landscape-mode. This should be totally app-store-safe.

This works for the gallery, not for taking pictures.

Share:
32,032

Related videos on Youtube

PF1
Author by

PF1

Updated on July 09, 2022

Comments

  • PF1
    PF1 almost 2 years

    I have been searching for an answer to this, but cannot come up with anything. Apparently, iPhone SDK 3.0 made it possible that UIImagePickerController can be displayed in landscape mode - but I am not finding any method that will allow this. I would think that if the application is in landscape by default it would automatically adjust the image controller, but that is not working for me.

    Thanks for any help!

    • kennytm
      kennytm over 14 years
      Given the built-in Photos app's image picker doesn't support landscape, your chance of having a landscape image picker is slim, and I can't think of an SDK-safe way to get the content of photo album…
    • Ron Srebro
      Ron Srebro over 14 years
      As KennyTM mentions, the built in photos app doesn't work in landscape mode. What made you think that SDK 3.0 made it possible to use the picker in landscape mode? Maybe if you share it with us it will give us a hint on how to go about it.
  • PF1
    PF1 over 14 years
    Hi erastusnjuki: Thanks for your reply! Can this be implemented in a UIViewController subclass, or does it require something else?
  • erastusnjuki
    erastusnjuki over 14 years
    It should though I haven't tested that.I implemented mine in a UITableViewController that adopts a <UIImagePickerControllerDelegate>.
  • PF1
    PF1 over 14 years
    Hi Alexandr: Even if I create the UIImagePickerController when the application is set to run in only landscape mode, it still shows up as vertically.
  • BlueDolphin
    BlueDolphin over 13 years
    Getting Data formatters temporary not available error during launch in iPhone 4.
  • user102008
    user102008 over 13 years
    "This will give you a warning. I ignored it." Apple might not be so happy about you using undocumented APIs, however
  • GeneCode
    GeneCode about 12 years
    Just to add, this is no longer a problem since iOS4. The ImagePickerController automatically shows itself in portrait format even when you're in landscape.
  • haifacarina
    haifacarina over 11 years
    It works in making the orientation stay landscape right but.. crashes the app. :(
  • Nate
    Nate about 11 years
    @DmitryKhryukin, this is a private API, and will almost certainly be rejected in an App Store app (unless you want to try to obfuscate the setOrientation: selector, and sneak it in.)
  • Dmitry Khryukin
    Dmitry Khryukin about 11 years
    @Nate yes, I understand this. That's why I asked these questions.
  • Nate
    Nate about 11 years
    @DmitryKhryukin, and that's why I answered it. Apple won't accept this, unless you can hide the way you use it.
  • d4Rk
    d4Rk about 11 years
    I managed to get the picker showing up in landscape, BUT the camera (live) image is 90° rotated, so you can't really use this to take any photos, as if you move the iPhone horizontal you will see a vertical movement in the camera screen / image and the other way round. Did someone else face this problem or even has a fix for that? Thanks in advance for any help! :)
  • Jayprakash Dubey
    Jayprakash Dubey over 10 years
    Getting error as - No visible @interface for 'UIDevice' declares the selector 'setOrientation:'
  • Jayprakash Dubey
    Jayprakash Dubey over 10 years
    Getting warning as - Implicit conversion from enumeration type 'enum UIInterfaceOrientation' to different enumeration type 'UIDeviceOrientation' (aka 'enum UIDeviceOrientation')
  • Jayprakash Dubey
    Jayprakash Dubey over 10 years
    Solution for above error is to use following code [At symbol]interface UIDevice () -(void)setOrientation:(UIDeviceOrientation)orientation; [At symbol]end
  • Jayprakash Dubey
    Jayprakash Dubey over 10 years
    Really...this one saved my entire project re-work and thanks a lots!!
  • AsifHabib
    AsifHabib about 10 years
    iOS 7 is the main problem! every thing was working fine in iOS 6
  • Muruganandham K
    Muruganandham K over 9 years
    Rasing Error : No visible @interface for 'UIApplication' declares the selector 'setOrientation:'
  • Jeff
    Jeff about 8 years
    I know its 3 years later but your project shows a black screen. Just letting you know.
  • reggian
    reggian about 8 years
    Thanks, I'll add it to my ToDo list:)
  • LeXeR
    LeXeR almost 8 years
    i would make this as accepted answer. Use picker.modalPresentationStyle = UIModalPresentationCurrentContext; for better results.
  • Roman B.
    Roman B. almost 7 years
    @LeXeR works great! Also, UIModalPresentationCustom will fix landscape presentation on iPad.
  • Dan Selig
    Dan Selig over 6 years
    swift: picker.modalPresentationStyle = .currentContext