1

我需要帮助才能让这段代码在 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];
  }
4

3 回答 3

2

您可以在 iPhone中显示UIImagePicker为 a 。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。

于 2012-11-23T17:35:52.970 回答
2

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

于 2012-11-23T17:04:55.720 回答
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]; 
    }


}
于 2013-08-17T21:54:54.597 回答