我已经创建了从摄像机捕获图像的应用程序。 这是我的代码
-(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'
关于异常,错误信息是非常明确的。 “在iPad上,的UIImagePickerController必须通过UIPopoverController呈现”为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提到的,摄像头可以呈现模态。 上述适用于当sourceType
是UIImagePickerControllerSourceTypePhotoLibrary
。
@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;
对于斯威夫特与iOS11:
videoEditor.modalPresentationStyle = .popover
self.present(picker, animated: true)
let popover = picker.popoverPresentationController
popover?.sourceView = self.view
// then config popover frame and arrow style here
文章来源: Exception in iPad, UIImagePickerController must be presented via UIPopoverController