Henry F
Henry F

Reputation: 4980

iOS: UIImagePickerController Issue

I'm trying to use UIImagePickerController to grab a photo from the users Photos on their iPhone / iPad. This code works just fine for iPhone, but when I run it on iPad, the debugger gives me the message "Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'On iPad, UIImagePickerController must be presented via UIPopoverController'". I'm very new to Objective-C, so I'm unsure of how to edit this code to use UIPopoverController when its being run on the iPad. I'd rather not create 2 new View Controllers, so I was wondering if someone knows what code I would need to add in to have it work on both the iPhone and iPad. Here is the code in my view controllers:

ViewController.h:

    @interface PhotoViewController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate> {
        UIButton *grabButton;
        UIImageView *image;
        UIImagePickerController *imgPicker;
    }
    @property (strong, nonatomic) IBOutlet UIButton *grabButton;
    @property (strong, nonatomic) IBOutlet UIImageView *image;
    @property (strong, nonatomic) UIImagePickerController *imgPicker;

    - (IBAction)grabImage;

    @end

ViewController.m:

    #import "PhotoViewController.h"


    @implementation PhotoViewController
    @synthesize grabButton;
    @synthesize image;
    @synthesize imgPicker;

    - (IBAction)grabImage {
        [self presentModalViewController:self.imgPicker animated:YES];
    }

    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {
        image.image = img;
        [[picker parentViewController] dismissModalViewControllerAnimated:YES];
    }

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            self.title = NSLocalizedString(@"Photo Gallery", @"Photo Gallery");
            self.tabBarItem.image = [UIImage imageNamed:@"42-photos.png"];
        }
        return self;
    }

    - (void)viewDidLoad
    {
        self.imgPicker = [[UIImagePickerController alloc] init];
        self.imgPicker.allowsImageEditing = YES;
        self.imgPicker.delegate = self;
        self.imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [super viewDidLoad];
    }

Thanks in advance!

Upvotes: 0

Views: 3646

Answers (3)

Hitesh Vaghela
Hitesh Vaghela

Reputation: 1635

It's worked for me. Please try below code

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
    UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:self.imgpicker];
    popover.delegate =self;
    [popover presentPopoverFromRect:self.view.bounds inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];}else{
[self presentModalViewController:self.imgpicker animated:YES];
}

Upvotes: 0

aryaxt
aryaxt

Reputation: 77596

Like Legolas mentioned, in an iPad app you must use a UIPopOverController in order to present an imagePicker. I'm usually not a fan of having any code in my app that performs tasks based on device type, but if you don't find a better solution you can do the following.

if ([[UIDevice currentDevice].model isEqual:@"iPad"])
{
   // Display image picker in a popover
}
else
{
   // display imagePicker as a modal
}

Check the documentation for device models: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIDevice_Class/Reference/UIDevice.html

Upvotes: 2

Legolas
Legolas

Reputation: 12325

You have the answer through that error message !

Use UIPopOverController with UIImagePicker for iPad.

If you want to know how to use UIPopOverController, you can look at this tutorial !

Another youtube tutorial - http://www.youtube.com/watch?v=6Gc3kxVwfmE

Upvotes: 6

Related Questions