IOS 在Ipad 横屏 上使用UIImagePickerController

时间:2020-12-28 16:59:30

转载前请注明来源:http://www.cnblogs.com/niit-soft-518/p/4381328.html

最近在写一个ipad的项目,该项目必须是横屏。进入正题,有一项功能是要调用系统的牌照和相册,于是本屌用了 UIImagePickerController,兴冲冲的运行了一下,呵呵,崩掉了,然后带着高逼格去查苹果文档,一堆英文,不懂,好吧,百度一下,发现好多同道中人说 UIImagePickerController必须要竖屏,不能横屏~这让我差点泪崩,又继续看苹果文档,发现了新大陆:

  1. Present the user interface. On iPhone or iPod touch, do this modally (full-screen) by calling the presentViewController:animated:completion: method of the currently active view controller, passing your configured image picker controller as the new view controller.

    On iPad, the correct way to present an image picker depends on its source type, as summarized in this table:

     

    Camera

    Photo Library

    Saved Photos Album

    Use full screen

    Must use a popover

    Must use a popover

    The table indicates that on iPad, if you specify a source type of  UIImagePickerControllerSourceTypePhotoLibrary or UIImagePickerControllerSourceTypeSavedPhotosAlbum, you must present the image picker using a popover controller, as described in Presenting and Dismissing the Popover. If you attempt to present an image picker modally (full-screen) for choosing among saved pictures and movies, the system raises an exception.

    On iPad, if you specify a source type of UIImagePickerControllerSourceTypeCamera, you can present the image picker modally (full-screen) or by using a popover. However, Apple recommends that you present the camera interface only full-screen.

好吧,以我四级未过的英语水平就不做翻译了,大概的意思就是说 如果在ipad中用uiimagepickercontroller,如果调用本地相册,必须使用POPOverController,如果要调用本地相机,可是将modalPresentationStyle属性设置为UIModalPresentationFullScreen也可以设置为popover,苹果推荐设置为全屏的fullscreen。设置然后我就试着写了一下,果然很OK!

以下是代码:

  一个调用相册的功能:

/*
相册
*/
-(void)Photo:(UIButton *)sender{
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
// 编辑模式
imagePicker.allowsEditing=NO;
UIPopoverController *pop=[[UIPopoverController alloc]initWithContentViewController:imagePicker];
// [self presentViewController:imagePicker animated:YES completion:NULL];
[pop presentPopoverFromRect:sender.bounds inView:sender permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];
}

效果:

IOS 在Ipad 横屏 上使用UIImagePickerController

iOS8以下推荐使用

    UIAlertAction *action_one = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
NSLog(@"打开相册");
[imgPicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
imgPicker.modalPresentationStyle = UIModalPresentationPopover; //调用本地相册,必须使用popover
popPC =imgPicker.popoverPresentationController;
[popPC setBarButtonItem:[[UIBarButtonItem alloc]initWithCustomView:bt]];
popPC.permittedArrowDirections = UIPopoverArrowDirectionAny;
popPC.delegate=self;
[self presentViewController:imgPicker animated:false completion:nil];
}else{
[SVProgressHUD showErrorWithStatus:@"请前往设置界面允许程序访问相册!"];
}
}];
[sheet addAction:action_one];
UIAlertAction *action_two = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
NSLog(@"打开相机");
[imgPicker setSourceType:UIImagePickerControllerSourceTypeCamera];
imgPicker.modalPresentationStyle = UIModalPresentationFullScreen; //苹果推荐使用全屏
popPC =imgPicker.popoverPresentationController;
[popPC setBarButtonItem:[[UIBarButtonItem alloc]initWithCustomView:bt]];
[popPC setSourceRect:CGRectMake(, , , )];
popPC.permittedArrowDirections = UIPopoverArrowDirectionAny;
popPC.delegate=self;
[self presentViewController:imgPicker animated:false completion:nil];
}else{
[SVProgressHUD showErrorWithStatus:@"请前往设置界面允许程序访问摄像头!"];
}
}];