首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 平面设计 > 图形图像 >

UIImagePickerController在iPhone跟iPad上的区别

2012-06-27 
UIImagePickerController在iPhone和iPad上的区别在iPhone中获取照片库的常用方法如下:?UIImagePickerContr

UIImagePickerController在iPhone和iPad上的区别

在iPhone中获取照片库的常用方法如下:

?

UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypePhotoLibrary]) {imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;imagePicker.delegate = self;[imagePicker setAllowsEditing:NO];[self presentModalViewController:imagePicker animated:YES];[imagePicker release];} else {UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"Error accessing photo library!" delegate:nil cancelButtonTitle:@"Close" otherButtonTitles:nil];[alert show];[alert release];}

?

这在iPhone下操作是没有问题的,但在iPad下就会有问题了,运行时会报出下面的错误:

?

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'On iPad, UIImagePickerController must be presented via UIPopoverController'

?

所以,我们必须通过UIPopoverController来实现才行。具体的实现如下:

?

UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypePhotoLibrary]) {imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;imagePicker.delegate = self;[imagePicker setAllowsEditing:NO];UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:imagePicker];self.popoverController = popover;[popoverController presentPopoverFromRect:CGRectMake(0, 0, 300, 300) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];[popover release];[imagePicker release];} else {UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"Error accessing photo library!" delegate:nil cancelButtonTitle:@"Close" otherButtonTitles:nil];[alert show];[alert release];}

?

热点排行