UIImagePickerController InterfaceOrientation Crash
Since upgrading my device to 6.1, I am getting crashes when trying to show the UIImagePickerController. I am only using the orientation of Portrait.
Failure:
Reason: * Application terminated due to uncaught exception 'UIApplicationInvalidInterfaceOrientation', reason: 'preferredInterfaceOrientationForPresentation must return supported interface orientation!'
Here I am calling UIImagePickerController:
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
//The device cannot make pictures
[PMAlertDialog showWithTitle:NSLocalizedString(@"incompatibleDeviceDialogTitle", nil) message:NSLocalizedString(@"incompatibleDeviceDialogMessage", nil) andButtonTitle:NSLocalizedString(@"okButtonTitle", nil)];
return;
}
if (_imagePicker == nil)
{
_imagePicker = [[UIImagePickerController alloc] init];
_imagePicker.delegate = self;
}
_imagePicker.allowsEditing = NO;
_imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
_imagePicker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentModalViewController:_imagePicker animated:YES];
I added these methods to the view controller where the UIImagePickerController is added:
-(BOOL)shouldAutorotate
{
return NO;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
source to share
To fix the problem, I made the following category:
I created a new objective-c class, "UIImagePickerController + NonRotating"
In the header file (UIImagePickerController + NonRotating.h):
#import <Foundation/Foundation.h>
@interface UIImagePickerController (NonRotating)
- (BOOL)shouldAutorotate;
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation;
@end
In the implementation file (UIImagePickerController + NonRotating.m):
#import "UIImagePickerController+NonRotating.h"
@implementation UIImagePickerController (NonRotating)
- (BOOL)shouldAutorotate {
return NO;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationPortrait;
}
@end
You can of course change this, but you see fit - make it autorotable and return multiple supported orientations, etc.
source to share