问题
-(IBAction)selectPressed:(id)sender
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
[picker release];
}
I am testing this code on iPad and iPhone simulators. In the iPhone simulator (and on real iPhones too) it's ok - gallery appears. But on the iPad simulator (I don't have a device), it crashes. Any ideas why?
回答1:
Please read the exception messages in the device log:
On iPad, UIImagePickerController must be presented via UIPopoverController
回答2:
Max said:
On iPad, UIImagePickerController must be presented via UIPopoverController
It now appears that we can presentModalViewController the UIImagePickerController when its sourceType is set to UIImagePickerControllerSourceTypeCamera. This must be to support the iPad 2's cameras in a full screen view. Max is correct that presentModalViewController crashes on iPads when sourceType is set to anything else.
回答3:
When displaying a modal view controller on the iPad, that view controller also needs it's modalPresentationStyle
property to be set in order to display the incoming view.
Here are the options available to you from the documentation:
typedef enum {
UIModalPresentationFullScreen = 0,
UIModalPresentationPageSheet,
UIModalPresentationFormSheet,
UIModalPresentationCurrentContext,
} UIModalPresentationStyle;
回答4:
try using this code for ipad photolibrary,
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
popover = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
[popover presentPopoverFromRect:CGRectMake(0.0, 0.0, 400.0, 400.0)
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
or http://www.techotopia.com/index.php/An_Example_iOS_4_iPad_Camera_and_UIImagePickerController_Application_(Xcode_4) reference..
来源:https://stackoverflow.com/questions/5046519/uiimagepickercontroller-crashes-in-ipad