1

I am trying to open a UIImagePickerController object in my application using this code:

   self.imgPicker = [[UIImagePickerController alloc] init];
   self.imgPicker.delegate = self;
   self.imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
   self.imgPicker.modalPresentationStyle = UIModalPresentationFullScreen;
   [self presentViewController:self.imgPicker animated:YES completion:nil];

But then the application crashes and displays the following error:

Terminating app due to uncaught exception 'UIApplicationInvalidInterfaceOrientation', reason: 
'preferredInterfaceOrientationForPresentation must return a supported interface orientation!'

I also added following methods in my ViewController class

- (BOOL) shouldAutorotate
{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:    (UIInterfaceOrientation)toInterfaceOrientation {

    return UIInterfaceOrientationMaskPortrait;
}

So, what else should I do to present this UIImagePickerController?

Roland Keesom
  • 8,180
  • 5
  • 45
  • 52
ChandreshKanetiya
  • 2,414
  • 5
  • 27
  • 41
  • check this http://stackoverflow.com/questions/12690963/preferredinterfaceorientationforpresentation-must-return-a-supported-interface-o – Manthan Aug 12 '13 at 07:30

4 Answers4

1

This seems to be because the choose-photo VC doesn't implement the new orientation methods. To fix this, make sure for root view controller does implement them, even when the top VC doesn't.

In code:

@implementation UINavigationController (Rotation_IOS6)
-(BOOL)shouldAutorotate {
    return [self.topViewController shouldAutorotate];
}
-(NSUInteger)supportedInterfaceOrientations {
    UIInterfaceOrientationMask topControllerOrientationMask = [self.topViewController supportedInterfaceOrientations];
    return topControllerOrientationMask ? topControllerOrientationMask : UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    UIInterfaceOrientation topControllerOrientation = [self.topViewController preferredInterfaceOrientationForPresentation];
    return topControllerOrientation ? topControllerOrientation : UIInterfaceOrientationPortrait;
}
@end
Yonat
  • 4,382
  • 2
  • 28
  • 37
0

About the crash problem you can read Here

Also shouldAutorotateToInterfaceOrientation must return not UIInterfaceOrientationMaskPortrait, but UIInterfaceOrientationPortrait, about the difference you can read Here

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return UIInterfaceOrientationPortrait;
}
Community
  • 1
  • 1
B.S.
  • 21,660
  • 14
  • 87
  • 109
0

I think your trying to launch image picker your app on iPad. If it is so, you cannot launch picker this type on iPad, it works only on iPhone. For using ImagePicker on iPad, launch imagePicker using UIPopOver.

Satish Azad
  • 2,302
  • 1
  • 16
  • 35
0

I have same problem few days ago. I found this following trick.

Put this code in your

AppDelegate.m

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    return UIInterfaceOrientationMaskAll;
}

And also put this code in your UIImagePickerViewController class.

- (NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskLandscape;
}

Because in iOS-6 UIImagePickerController must be follow portrait orientation.

iPatel
  • 46,010
  • 16
  • 115
  • 137