我在这个问题上研究了很多。触摸按钮时,我必须从设备中选择图像并显示在屏幕上。
但当按钮被触摸/按下时,它会导致我的应用程序崩溃。它从下面这行代码崩溃:
[self presentModalViewController:myPhotopicker animated:YES];我正在使用Xcode4.2开发一个iPad应用程序。我正在使用iPad 5.0模拟器进行测试。我的系统运行在Mac 10.6.8版本上。
当按下按钮时,将调用以下函数:
-(IBAction)getPhoto:(id)sender
{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
if (myPhotopicker==nil) { myPhotopicker = [[UIImagePickerController alloc] init];
myPhotopicker.delegate = self; }// create once!
myPhotopicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:myPhotopicker animated:YES];
} else {
NSString *str = @"Photo Album is not available!";
}
}发布于 2012-05-08 22:04:41
我试过你的代码,可以在模拟器中重现崩溃的过程。但它在我的iPhone 4和iOS 4.2上工作得很好。
也就是说,我在模拟器中的图库中包含了一些照片。(启动模拟器Safari,打开一些页面,通过长按并从菜单中选择保存来保存一些图片。)
现在,模拟器编写
2012-05-08 15:53:55.605 test[5870:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'On iPad, UIImagePickerController must be presented via UIPopoverController'到控制台。
好的,读完,完成:
-(IBAction)getPhoto:(UIButton *)sender
{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
{
if (myPhotopicker==nil) {
myPhotopicker = [[UIImagePickerController alloc] init];
myPhotopicker.delegate = self;
}// create once!
myPhotopicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
// iPad Code:
UIPopoverController *popover =
[[UIPopoverController alloc] initWithContentViewController:myPhotopicker];
[popover presentPopoverFromRect:sender.bounds
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
} else {
// iPhone Code:
[self presentModalViewController:myPhotopicker animated:YES];
}
} else {
NSLog(@"Photo Album is not available!");
}
}现在看起来是这样的:

https://stackoverflow.com/questions/10498574
复制相似问题