Skip to main content

Objective-C workaround when accessing the iPad camera roll

Objective-c

I ran into a strange problem the other day while I was working. I was developing a iPad photo application that included the functionality of selecting photos from your camera roll and taking images from the device's camera. I first developed the functionality to pull up the camera, take a photo, and save it to a UIImageView in the ViewController, this worked great and I did not think anything was up. However, when I developed the functionality to pull up the camera roll and select a photo to assign to the UImageView, the app kept crashing with a strange error I'd never seen before. Here is the error:


 
"Terminating app due to uncaught exception 'UIApplicationInvalidInterfaceOrientation', reason: 'Supported orientations has no common orientation with the application, and shouldAutorotate is returning YES."
 
I was puzzled and tried to remedy the error by creating a category for the UIImagePickerController to return NO for the method, shouldAutorotate. This did not work. I tried a couple more solutions with no evail. Finally, I figured out the reason I was receiving this error is becuase one of the specs on the iPad application is that it needed to be locked landscape and the ViewController that carries the UIImagePickerController can only be displayed in portrait. There were many developers claiming they had overcome this issue on stackoverflow, but I was unable to implement any solution that provided resolution. In the end I had implement a workaround that opened a UIPopoverController with the UIImagePickerController inside it. Almost exactly the same way Facebook implements this solution on their iPad application. Below I included a coding example of the workaround.

Implementation of the iPadViewController headed file.

@interface iPadViewController : UIViewController {
    UIButton *activateCameraRoll;
}
@property (nonatomic, strong) UIPopoverController *cameraRoll;
@end

Implementation of main file declaring the UIButton to trigger the UIPopoverController loaded with the UIImagePickerController.

@implementation iPadViewController
@synthesize cameraRoll;
 
-(void)viewDidLoad{
   //implement a button to activate the camera roll
    activateCameraRoll = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [activateCameraRoll setFrame:CGRectMake(50, 50, 120, 36)];
    [activateCameraRoll addTarget:self action:@selector(openCameraRoll:)forControlEvents:UIControlEventTouchDown];
    activateCameraRoll.showsTouchWhenHighlighted = YES;
    [self.view addSubview:activateCameraRoll ];
 
}
//declare an UIImagePickerController so that the UIPopoverController can be opened with it
-(void)openCameraRoll:(id)sender
{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
 
    UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:picker];
    [popover presentPopoverFromRect:((UIButton *)sender).bounds inView:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    //assign a reference to the strongly declared UIPopoverController
    self.cameraRoll = popover;
}
//delegate function that receives the UIImage that is selected by the user
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info {
    UIImage *image = [info objectForKey: UIImagePickerControllerOriginalImage];
}
@end

Member for

3 years 9 months
Matt Eaton

Long time mobile team lead with a love for network engineering, security, IoT, oss, writing, wireless, and mobile.  Avid runner and determined health nut living in the greater Chicagoland area.