1

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 'NSGenericException', reason: '-[UIPopoverController dealloc] reached while popover is still visible.". I'm very new to Objective-C, so I'm unsure of whats causing this, I do not dealloc anything and I have ARC turned on. Here is my code: ViewController.m

#import "PhotoViewController.h"


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

- (IBAction)grabImage {
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
        UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:imgPicker];
        [popover presentPopoverFromRect:self.image.bounds inView:self.image permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

    } else {
        [self presentModalViewController:imgPicker animated:YES];
    }
}

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

- (void)viewDidLoad
{
    self.imgPicker = [[UIImagePickerController alloc] init];
    self.imgPicker.allowsImageEditing = YES;
    self.imgPicker.delegate = self;
    self.imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

}
Ryan Last
  • 65
  • 1
  • 5

1 Answers1

3

UIPopover is an ugly patchwork beast of an object. It must be a strong property and an iVar to ensure that Dealloc isn't reached prematurely. Add this in the .h Like so:

@interface MyClass: NSObject {
    UIPopover *_popover;
}
@property (nonatomic, strong) UIPopover * popover;

//.m 

@synthesize popover = _popover;

When you instantiate the popover, assign it to either the property or the instance:

self.popover = [[UIPopoverController alloc] initWithContentViewController:imgPicker];

or

_popover = [[UIPopoverController alloc] initWithContentViewController:imgPicker];
Carl Veazey
  • 18,392
  • 8
  • 66
  • 81
CodaFi
  • 43,043
  • 8
  • 107
  • 153
  • Thank you so much! Which class should I delete though? Sorry I'm new to programming. Also, I added in all of the changes that you posted above, but the same error is still occurring. – Ryan Last Mar 26 '12 at 04:32
  • When you add properties or iVars (like we did above) you must delete any iVars with conflicting names, or they'll overshadow the iVar you declared in the .h. Just delete any mention of UIPopover (the word, not the objects), from your .m file. – CodaFi Mar 26 '12 at 04:44
  • Except for alloc (sorry, should have mentioned that). So, it will look like this: `popover = [[UIPopoverController alloc] initWithContentViewController:imgPicker];` – CodaFi Mar 26 '12 at 04:52
  • Ah alrighty thanks, its working great now! Thank you so much for all your help! – Ryan Last Mar 26 '12 at 04:56
  • No problem. Welcome to the stack! – CodaFi Mar 26 '12 at 04:57