1

I'm having a very strange behavior:

  1. in iOS 5 I present UIImagePickerController in this way:

imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera; imagePicker.modalPresentationStyle = UIModalPresentationFullScreen; imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;

[self presentModalViewController:imagePicker animated:YES];

now in iOS 6 this produce a crash. I resolved the crash by writing a category on UIImagePickerController:

@implementation UIImagePickerController (NonRotating)

- (BOOL)shouldAutorotate
{
    return NO;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationMaskPortrait;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;
}

@end

The problem is that now the UIImagePickerController doesn't rotate and it's shown up-side. Moreover, when I press the "cancel" button and the picker is dismissed, the app crash again.

  1. If I use the UIImagePickerController inside a UIPopoverController, all works fine (belonging to the fact that the popover doesn't rotate) but when I dismiss the popover ALL view controller in my app stop responding to rotation events and this cause that all app is blocked in this orientation. To restore the correct behavior I need to quit the app from the background and open again.

This is the code I'm using to display popover

_cameraPopoverController = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
[_cameraPopoverController presentPopoverFromRect:_takeFromCamera.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

This problem drive me crazy !

Fry
  • 6,235
  • 8
  • 54
  • 93
  • `presentModalViewController:animated:` is deprecated in iOS 6. This should not crash your app, but maybe you should try `presentViewController:animated:completion:` instead. – Jonathan Cichon Jan 17 '13 at 16:15

2 Answers2

0

What is your picker source type? Photo library/album or camera roll?

Assuming that you are using Photo Library / Album source, on iPad, you MUST use a popover:

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIImagePickerController_Class/UIImagePickerController/UIImagePickerController.html (look in the overview, point 4)

presenting it fullscreen is not supported.

About the other issue (after dismissing the popOver, the other VC's stops rotating) check that you have a STRONG reference to your popover (strong property). Paste the code you are using to present the popover.

LombaX
  • 17,265
  • 5
  • 52
  • 77
  • I use UIImagePicker controller to take a photo from camera. I edit my question with code. – Fry Jan 18 '13 at 09:14
  • Mmmhh, ok, so you can present it modally or in popOver, it's the same. First thing I would do: try to understand why it's crashing when you present it *without* the category. Do you see anything on the log? What happens if you put an exception breakpoint? Moreover, that category doesn't seem a good solution, the methods you are trying to override in the category are, probably, yet implemented in the original UIImagePickerController. Doing this will give you unexpected behavior. See this http://stackoverflow.com/questions/5272451/overriding-methods-using-categories-in-objective-c – LombaX Jan 18 '13 at 11:05
  • This is the log when app crashes **Terminating app due to uncaught exception 'UIApplicationInvalidInterfaceOrientation', reason: 'preferredInterfaceOrientationForPresentation must return a supported interface orientation!** – Fry Jan 18 '13 at 13:28
  • This shouldn't happen :-) I suspect that something in your app code is causing it. Try to do this: Create a new project, iPad only. Choose the single view template. Then, in the ViewController class add this code and check if the picker appears: `- (void) viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; UIImagePickerController *picker = [[UIImagePickerController alloc]init]; picker.sourceType = UIImagePickerControllerSourceTypeCamera; [self presentViewController:picker animated:YES completion:nil]; }` – LombaX Jan 18 '13 at 14:02
  • I've tried in the same project in another view controller and it's work. Maybe the problem is that I show the picker from a modal view controller in pagesheet style ? – Fry Jan 21 '13 at 09:49
  • I don't see any indication in the doc about the presentation style supported by UIImagePickerController. But reading this: "On iPad, if you specify a source type of UIImagePickerControllerSourceTypeCamera, you can present the image picker modally (full-screen) or by using a popover. However, Apple recommends that you present the camera interface only full-screen." --> there is a part where it says "modally (full-screen)". It could mean that, if is presented modally, must be fullscreen? I think that you can try to change the presentation style...it's only a line of code ;-) – LombaX Jan 21 '13 at 15:19
0

While I don't recommend using the category to override the image picker's default behavior, there is a bug in the implementation that causes the crash mentioned:

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationMaskPortrait;
                                 ~~~~
}

The return value shouldn't be an orientation mask, it should be an orientation, e.g. UIInterfaceOrientationPortrait.

Rhult
  • 1,646
  • 15
  • 10