6

Please note that the answer below - do not work for iOS6 so I still need an answer!

My application is enabled only for Portrait mode.

However, if I embed a UIImagePickerController inside as a subview, and rotate the device, the top and bottom bar stays in the same location, however UIImagePickerController does rotate.

How can I prevent it from rotating?

This is the code:

    [self.view.window addSubview:self.imagePickerController.view];
    self.imagePickerController.showsCameraControls = NO; 
    self.imagePickerController.view.frame = CGRectMake(0, 90, 320, 320);
    self.imagePickerController.allowsEditing = NO;

EDITED

I am using iOS6 where shouldAutorotate is not being calle

Dejell
  • 13,947
  • 40
  • 146
  • 229
  • Please take a look at: http://stackoverflow.com/questions/538041/uiimagepickercontroller-camera-preview-is-portrait-in-landscape-app – C0D3 Feb 28 '14 at 17:24

4 Answers4

12

Add this UIImagePickerController category in your class,

@interface UIImagePickerController(Nonrotating)
- (BOOL)shouldAutorotate;
@end

@implementation UIImagePickerController(Nonrotating)

- (BOOL)shouldAutorotate {

  return NO;
}

@end
Anusha Kottiyal
  • 3,855
  • 3
  • 28
  • 45
  • 1
    What do you mean by adding in your class? Could it be a separate file? if so - how do I refer to it? – Dejell Jan 21 '13 at 11:44
  • It doesn't work. When I rotate the iPhone, also the UIImagePickerController rotates. It doesn't listen to any of the methods – Dejell Jan 21 '13 at 11:51
  • @Odelya Did you add UIImagePickerControllerDelegate on .h file?? – Anusha Kottiyal Jan 21 '13 at 11:52
  • Sure! didFinishPickingMediaWithInfo is being called after capturing an image – Dejell Jan 21 '13 at 11:56
  • I think http://stackoverflow.com/questions/12775265/ios-6-shouldautorotate-is-not-being-called will help me – Dejell Jan 21 '13 at 11:58
  • Would you like to fix your answer so I could choose it? Since I don't understand how to use willRotateToInterfaceOrientation – Dejell Jan 21 '13 at 12:02
  • @Odelya I didn't use this willRotateToInterfaceOrientation before.. I will try.. Did you check you have deleted unwanted orientations from info.plist.? – Anusha Kottiyal Jan 21 '13 at 12:04
2

include the following in your controller this will work, I'm just creating the category of UIImagePickerController

@interface UIImagePickerController (private)

- (BOOL)shouldAutorotate;
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation;
- (NSUInteger)supportedInterfaceOrientations;
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation;

@end


@implementation UIImagePickerController (Private)

- (NSUInteger)supportedInterfaceOrientations {

    return UIInterfaceOrientationMaskPortrait;
}

- (BOOL)shouldAutorotate {

    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationPortrait;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{  
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
technerd
  • 14,144
  • 10
  • 61
  • 92
junaidsidhu
  • 3,539
  • 1
  • 27
  • 49
0

One possibility is to override the

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation;

method of UIImagePickerController. I'm not sure if this is the best possibility but it will work.

So if you only want your UIImagePickerController to be rotated to portrait use the following code

@interface PortraitUIImagePickerController : UIImagePickerController

@end

And the implementation should look like the following

@implementation PortraitUIImagePickerController

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return UIInterfaceOrientationIsPortrait(toInterfaceOrientation);
}

@end
who9vy
  • 1,091
  • 9
  • 19
0

The category in the most voted answer works, but since it is discouraged to use categories, you can also make a subclass of UIImagePickerController and use that.

If you want to avoid rotating of the UIImagePickerController add the following class

UINonRotatableImagePickerController.h

@interface UINonRotatableImagePickerController : UIImagePickerController

@end

UINonRotatableImagePickerController.m

@implementation UINonRotatableImagePickerController

- (BOOL)shouldAutorotate
{
    return NO;
}

@end

You have to change the UIImagePicker class in the storyboard to use UILandscapeImagePickerController, or if you allocate it in code, change

UIImagePickerController *picker = [[UIImagePickerController alloc] init];

to

UIImagePickerController *picker = [[UINonRotatableImagePickerController alloc] init];

and include UINonRotatableImagePickerController.h in your code.

masam
  • 2,268
  • 1
  • 22
  • 24
  • imagePicker is Always Portrait and never support subclassing, It rotates only if image picker is added as subview because it rotates because of superview, not itself. – AsifHabib Apr 08 '14 at 13:14
  • Maybe that is the case for the iPhone, but I have my fullscreen imagepicker in landscape for the iPad. – masam Apr 10 '14 at 07:37