如何让这个图像选择器在iPad上运行?

时间:2012-11-23 16:59:28

标签: iphone objective-c ios xcode

我需要帮助让这些代码在iPad上运行,它可以在iPhone上运行良好,但出于任何理由不是iPad。我不知道该怎么做才能让这个图像选择器在iPad上运行。任何帮助将不胜感激。

  -(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];
  }

3 个答案:

答案 0 :(得分:2)

在iPad上,您需要在UIPopoverController中显示它,而不是以模态方式显示它。

答案 1 :(得分:2)

您可以在iPhone中将UIImagePicker显示为modalView。但在iPad中,您需要使用UIPopover作为显示imagePicker的容器。

重写您的代码,如:

-(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];
}

@interface中添加必要的协议和必要的实例

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

它适用于iPad和iPhone。

答案 2 :(得分:0)

你可以试试这个:

更改CGRectMake以获取自定义弹出窗口,并更改内容大小的CGSizeMake。

    - (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]; 
    }


}