您的位置:首页 > 产品设计 > UI/UE

UIImagePickerController在iPhone和iPad上的区别

2012-08-01 11:06 267 查看


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

}

转自:http://2015.iteye.com/blog/1340797
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: