0

我正在尝试通过单击按钮显示 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> {

有人可以看到我做错了什么吗?

4

3 回答 3

2

除了@Vikings 提出的好处之外,请务必在尝试使用之前检查您的设备是否有摄像头:

if ([UIImagePickerController
     isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
    [camera setSourceType:UIImagePickerControllerSourceTypeCamera];
} else {
    [camera setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}
于 2012-09-26T23:11:36.530 回答
1

确保您同时使用 Navigation Controller Delegate 和 Image Picker Controller Delegate。Image Picker 实际上是一个导航控制器,这就是为什么你必须实现它的委托。

 @interface YourViewController : UITableViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate>

此外,正确设置委托,而不是视图,而是视图控制器。

camera.delegate = self;

委托需要设置为 View Controller,而不是 View Controller 的 View。

查看下面的代码:

(1) 导航栏不需要隐藏,因为没有

(2) 不需要隐藏工具栏,因为没有

(3) 不需要指定 WantsFullScreenLayout,因为 Modal View Controller 会一直占据全屏

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
}
于 2012-09-26T23:01:31.287 回答
0

不要将您的代码放入 viewDidLoad 创建一个 IBAction 并将您的代码放入该函数中。

并将委托设置为您的视图控制器。并在 .h 文件中确保你写的

<UIImagePickerControllerDelegate>

[camera setDelegate : self];
于 2012-09-27T04:48:20.527 回答