moo
moo

Reputation: 37

How to to make this image picker work on iPad?

I need help getting this code to work on iPad, it works fine on iPhone but for what ever reason not iPad.I have no idea what to do get this image picker to work on iPad. Any help will be appreciated.

  -(IBAction)getCameraPicture:(id)sender
    {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsImageEditing = YES;
picker.sourceType = (sender == takePictureButton) ?    UIImagePickerControllerSourceTypeCamera :
UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentModalViewController: picker animated:YES];
[picker release];
     }

   -(IBAction)selectExitingPicture
{
if([UIImagePickerController isSourceTypeAvailable:
   UIImagePickerControllerSourceTypePhotoLibrary])
{
    UIImagePickerController *picker= [[UIImagePickerController alloc]init];
    picker.delegate = self;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentModalViewController:picker animated:YES];
    [picker release];
}


  }

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


   -(void)imagePickerControllerDidCancel:(UIImagePickerController *) picker
 {
[picker dismissModalViewControllerAnimated:YES];
  }

Upvotes: 1

Views: 3218

Answers (3)

oscar castellon
oscar castellon

Reputation: 3138

You can try this:

Change the CGRectMake for custom popover and CGSizeMake for content size.

    - (IBAction)imageFromAlbum:(id)sender
{
    imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;

    // es iPad
    if ([[UIDevice currentDevice]userInterfaceIdiom] == UIUserInterfaceIdiomPad) {

        //Averiguar si está en portrait o landscape
        UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];

        //PORTRAIT
        if(orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
        {

            [self cerrarTeclado];

            self.popover = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
            self.popover.delegate = self;

            [self.popover presentPopoverFromRect:CGRectMake(600, 400, 311, 350) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:NO];
            [self.popover setPopoverContentSize:CGSizeMake(330, 515)];

        }
        //LANDSCAPE
        if (orientation == UIInterfaceOrientationLandscapeRight || orientation == UIInterfaceOrientationLandscapeLeft)
        {

            self.popover = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
            self.popover.delegate = self;

            [self.popover presentPopoverFromRect:CGRectMake(850, 400, 311, 350) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:NO];
            [self.popover setPopoverContentSize:CGSizeMake(330, 515)];

        }

    } else {
       // no es iPad 
       [self presentViewController:imagePicker animated:YES completion:nil]; 
    }


}

Upvotes: 0

Midhun MP
Midhun MP

Reputation: 107131

You can display the UIImagePicker as a modalView in iPhone. But in iPad you need to use UIPopover as a container for displaying the imagePicker.

Re-write your code like:

-(IBAction)selectExitingPicture
{
  if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
  {
    UIImagePickerController *picker= [[UIImagePickerController alloc]init];
    picker.delegate = self;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
      self.popover = [[UIPopoverController alloc]
            initWithContentViewController:picker];
        popover.delegate = self;
        [self.popover presentPopoverFromRect:CGRectMake(0,0,170,250)
            permittedArrowDirections:UIPopoverArrowDirectionAny
            animated:YES];
    }
    else
    {
       [self presentModalViewController:picker animated:YES];
    }
   [picker release];
}

In your @interface add the necessary protocols and necessary instances

@interface yourController: UIViewController
<UIImagePickerControllerDelegate,
UINavigationControllerDelegate, UIPopoverControllerDelegate>
{
    UIPopoverController *popover;
}
@property (nonatomic, strong) UIPopoverController *popover;
@end

It'll work on both iPad and iPhone.

Upvotes: 2

Ismael
Ismael

Reputation: 3937

On iPad you need to show it inside a UIPopoverController, not present it modally.

Upvotes: 2

Related Questions