在iPad中,UIImagePickerController必须通过UIPopoverController呈现

时间:2013-02-07 06:23:02

标签: iphone ios objective-c ipad ios6

我已经从相机创建了一个捕获图像的应用程序。这是我的代码

 -(IBAction) showCameraUI {
    BOOL hasCamera = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
    UIImagePickerController* picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.sourceType = hasCamera ? UIImagePickerControllerSourceTypeCamera :    UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentModalViewController:picker animated:YES];
}

并实现此委托方法以获取捕获的图像

- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissModalViewControllerAnimated:YES];
    UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage];
    UIImage *yourImageView = image;
}

如果用户取消控制器

,则实施此方法
- (void)imagePickerControllerDidCancel:(UIImagePickerController*)picker
{
    [picker dismissModalViewControllerAnimated:YES];
}

但它显示了这个例外。有没有人知道为什么它在执行函数showCameraUI的最后一行后显示这样的异常。

UIStatusBarStyleBlackTranslucent is not available on this device. 2013-02-07 
10:06:06.976 CaptureImage[460:c07] *** Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: 'On iPad, UIImagePickerController must be 
presented via UIPopoverController'

2 个答案:

答案 0 :(得分:14)

关于异常,错误消息非常清楚。 “在iPad上,必须通过UIPopoverController呈现UIImagePickerController”对于iPad,您应该使用UIPopoverController而不是[self presentModalViewController:picker animated:YES];来呈现它。这应该可以解决问题。

例如: -

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
    UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:picker];
    [popover presentPopoverFromRect:self.view.bounds inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    self.popover = popover;
} else {
    [self presentModalViewController:picker animated:YES];
}

编辑如@rmaddy所述,相机可以模态呈现。 sourceTypeUIImagePickerControllerSourceTypePhotoLibrary时,以上内容适用。

答案 1 :(得分:5)

@Arun 我也面临同样的问题在头文件中添加全局属性。

我希望以下代码对您有用

UIImagePickerController *imgPicker = [[UIImagePickerController alloc] init];
   [imgPicker setDelegate:self];
   [imgPicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
   [imgPicker setAllowsEditing:YES];
   [imgPicker setModalPresentationStyle:UIModalPresentationCurrentContext];

   UIPopoverController *popOver = [[UIPopoverController alloc] initWithContentViewController:imgPicker];
   popOver.delegate = self;
   self.popoverImageViewController = popOver;
   [self.popoverImageViewController presentPopoverFromRect:CGRectMake(0, 0, 160, 40) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

在该头文件中创建像这样的全局属性

@property (strong) UIPopoverController *popoverImageViewController;