我正在开发一个使用cocos2d的iPad应用程序,我正在尝试让从库中选择一张照片,或者使用相机拍摄一张照片,以便在该应用程序中使用。昨天和今天,我疯狂地搜索了一整天,试图拼凑出一些东西来做上面的事情,但我可能应该从承认我真的不知道自己在做什么开始。
我现在让它工作的方式是,你点击当前的用户图像,它会弹出一个菜单,询问你是否要从相机或库中上传不同的照片。选择camera调用如下(库版本基本相同):
- (void) onGetCameraImage
{
UIImagePickerController* imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.allowsEditing = NO;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
[imagePicker presentViewController:[CCDirector sharedDirector] animated:YES completion:NULL];
}当我尝试并测试它时(当然,由于模拟器不能做摄像头,当然是因为模拟器不能做摄像头,是的,我确实打算实现对没有摄像头的检查),我得到了以下崩溃:
2013-04-07 19:21:50.248 prototype1[20897:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present modally an active controller <UIImagePickerController: 0x9e87ac0>.'我认为它在presentViewController调用中崩溃了,但我很难找出我做错了什么,因为所有的教程和stackoverflow答案都使用了弃用的模态函数调用!我怀疑我在之前的代码中做了各种疯狂的胡说八道?无论哪种方式,我都不知道如何让我的代码工作。
很多这样的代码都是从堆栈溢出中收集的,所以我不能保证其中任何一种的敏感性-我对iOS编码非常陌生,基本上是被投入到这个项目中的,我们只是试图让它正常工作。请帮帮我!
发布于 2013-04-08 10:55:22
是的,这里好像出了什么问题。为了实现实例化UIImagePickerController时所需的功能,您通常希望当前视图控制器呈现图像选取器实例。崩溃的发生是因为:
的内容
UIImagePickerController视图甚至还没有出现在屏幕上您有访问视图控制器的权限吗?
如果是这样的话,您可以简单地执行以下操作:
[self presentViewController:imagePicker animated:YES completion:nil];其中self是UIViewController的某个子类。
希望这能有所帮助!
发布于 2013-04-09 17:09:10
只需更改最后一行,如下所示
[imagePicker presentViewController: imagePicker animated:YES completion:nil];发布于 2013-04-08 11:59:10
在设置之前,还有一件事
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera您需要检查源类型是否可用。
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController *imagePicker =
[[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.allowsEditing = YES;
[self presentModalViewController:imagePicker animated:YES];
[imagePicker release];
}
else
{
//.....
} https://stackoverflow.com/questions/15870484
复制相似问题