iPad中的例外,UIImagePickerController必须通过UIPopoverController呈现

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

-(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函数的最后一行后显示这样的exception。

 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' 

关于例外情况,错误信息非常清楚。 “在iPad上,必须通过UIPopoverController呈现UIImagePickerController”对于iPad,您应该将其呈现在UIPopoverController而不是使用[self presentModalViewController:picker animated:YES]; 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时,以上情况适用。

@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]; 

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

 @property (strong) UIPopoverController *popoverImageViewController;