5
-(void)viewDidLoad
{
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
   {
      imagePicker = [[UIImagePickerController alloc] init];
      imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
      imagePicker.showsCameraControls = NO;
      [self.view addSubview:imagePicker.view];
      }
      else
      {
      // UIAlertView…
      }
}

    -(void)viewDidAppear:(BOOL)animated
    {
        [super viewDidAppear:animated];
        imagePicker.delegate = self;
        [self presentViewController:imagePicker animated:NO completion:NO];
    }

当您没有相机时,我想发出警报。iPhone 应用程序启动并移入此代码。但是,崩溃(此错误>

return UIApplicationMain(argc, argv, nil, NSStringFromClass([CameraAppDelegate class])); > Thread 1: signal SIGABRT)在模拟器中运行时。

为什么是这样?

4

2 回答 2

9

使用此代码并在文件中添加UIImagePickerControllerDelegate委托.h

 if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])  
    {
       UIImagePickerController* picker = [[UIImagePickerController alloc] init];
       picker.sourceType = UIImagePickerControllerSourceTypeCamera;
       picker.delegate = self;
      picker.wantsFullScreenLayout = YES;
      [self presentModalViewController:picker animated:YES];
    }
    else
    {
        UIAlertView *altnot=[[UIAlertView alloc]initWithTitle:@"Camera Not Available" message:@"Camera Not Available" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        altnot.tag=103;
        [altnot show];
        [altnot release];

    }
于 2012-12-04T09:08:28.170 回答
3

创建 NSObject 类并将其命名为ClsGlobal,或您想要的名称..

然后写+(BOOL)isCameraDeviceAvailable在你的ClsGlobal.h并在ClsGlobal.m.

+(BOOL)isCameraDeviceAvailable
{
    BOOL isCameraAvailable=NO;
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        if([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront] || [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear])
            isCameraAvailable = YES;
    }
    return isCameraAvailable;
}

使用此类方法,如果相机可用,它将返回 YES,否则返回 NO。

[ClsGlobal isCameraDeviceAvailable];现在您可以使用if Condition 看起来像这样的方式调用此方法if([ClsGlobal isCameraDeviceAvailable])

此方法将帮助您在任何控制器中的整个项目中,您只需导入 ClsGlobal like #import "ClsGlobal.h"

于 2012-12-04T09:22:40.277 回答