1

I am trying to write an app that will allow the user to select an image from the photo library on the iPad. I have done exactly as sources online say it should be done, however when I click the button only the arrow is showed on screen and nothing else. The popover which should show with the images does not show. Could anyone please tell me why this is happening? Here is my code:

        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        [picker setDelegate:self];

        [picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
        [picker setAllowsEditing:YES];

        popoverController = [[UIPopoverController alloc] initWithContentViewController:picker];
        [popoverController presentPopoverFromRect:self.view.frame inView:[self view] permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
user1145581
  • 1,017
  • 4
  • 13
  • 18

1 Answers1

0

I know it is to to late to answer but if you're already interested on it, this may work for you:

In the .h file, gives this delegates:

@interface ViewController : UIViewController <UIPopoverControllerDelegate, UINavigationControllerDelegate,  UIImagePickerControllerDelegate>

create the popover as a property:

@property (nonatomic, strong) UIPopoverController *popoverController;

and finally in the .m file:

-(IBAction)showPhotoLibray:(id)sender
{
    BOOL hasGellery = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary];
    UIImagePickerController* picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.sourceType = hasGellery ? UIImagePickerControllerSourceTypePhotoLibrary :    UIImagePickerControllerSourceTypePhotoLibrary;
    if (self.popoverController != nil)
    {
        [self.popoverController dismissPopoverAnimated:YES];
        self.popoverController=nil;
    }

    self.popoverController = [[UIPopoverController alloc] initWithContentViewController:picker];
    CGRect popoverRect = [self.view convertRect:[self.theViewObject frame]
                                       fromView:[self.theViewObject superview]];
    popoverRect.size.width = MIN(popoverRect.size.width, 300) ;
    popoverRect.origin.x = popoverRect.origin.x;
    [self.popoverController
     presentPopoverFromRect:popoverRect
     inView:self.view
     permittedArrowDirections:UIPopoverArrowDirectionAny
     animated:YES];
}

the self.theViewObject is an outlet of the object view controller that is calling the method, for example an UIButton

Luis Mejías
  • 301
  • 2
  • 10