我试图通过点击按钮显示UIImagePickerController。当我点击按钮时,我会收到一行SIGABRT:
[self presentModalViewController:camera animated:YES];
来自代码块:
camera = [[UIImagePickerController alloc]init];
[camera setSourceType:UIImagePickerControllerSourceTypeCamera];
[camera setDelegate:self.view];
camera.showsCameraControls = NO;
camera.navigationBarHidden = YES;
camera.wantsFullScreenLayout = YES;
camera.toolbarHidden = YES;
camera.cameraOverlayView = bottomArrow;
[self presentModalViewController:camera animated:YES];
其中camera
是UIImagePickerController
的名称,定义如下:
UIImagePickerController *camera;
@interface
中的。 我的界面声明是:
@interface cameraViewController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate> {
有人能看出我做错了吗?
答案 0 :(得分:1)
确保使用导航控制器代理和图像选择器控制器代理。图像选择器实际上是一个导航控制器,这就是你必须实现其委托的原因。
@interface YourViewController : UITableViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate>
此外,正确设置委托,而不是视图,但视图控制器。
camera.delegate = self;
需要将委托设置为View Controller,而不是View Controller的View。
查看以下代码:
(1)您不需要隐藏导航栏,因为没有一个
(2)您不需要隐藏工具栏,因为没有一个
(3)您不需要指定wantsFullScreenLayout,因为模态视图控制器将始终占据整个屏幕
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.showsCameraControls = NO;
// Comment out the line below to make sure it is not causing a problem.
// This just expects a view, so if bottomArrow is a view you should be fine
picker.cameraOverlayView = bottomArrow;
[self presentModalViewController:picker animated:YES];
另外,我没有意识到你在viewDidLoad中加载了这个代码,这会崩溃,因为View Controller本身没有完成它的转换,所以你不能开始另一个转换。而是使用viewDidAppear获得相同的效果:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:YES];
// Place code here
}
答案 1 :(得分:1)
除了@Vikings提出的好处之外,在尝试使用之前,请务必检查您的设备是否有相机:
if ([UIImagePickerController
isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
[camera setSourceType:UIImagePickerControllerSourceTypeCamera];
} else {
[camera setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}
答案 2 :(得分:0)
不要将你的代码放在viewDidLoad中创建一个IBAction并将你的代码放在该函数中。
并将委托设置为视图控制器。并在.h文件中确保您写的
<UIImagePickerControllerDelegate>
[camera setDelegate : self];