UIImagePickerController,Check camera

一世执手 提交于 2019-12-10 14:48:21

问题


-(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];
    }

I want to put out an alert when you do not have a camera. IPhone app launch and move in this code. But, Crash (This Error >

return UIApplicationMain(argc, argv, nil, NSStringFromClass([CameraAppDelegate class])); > Thread 1: signal SIGABRT) when run in a simulator.

Why is this?


回答1:


use this code and add UIImagePickerControllerDelegate delegate in .h file

 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];

    }



回答2:


Create NSObject Class and name it like ClsGlobal, or the name you want..

then write +(BOOL)isCameraDeviceAvailable in your ClsGlobal.h and implement below function in ClsGlobal.m.

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

Use this class method, It will return YES if camera available else return NO.

Now you can call this method using [ClsGlobal isCameraDeviceAvailable]; means your if Condition looks like if([ClsGlobal isCameraDeviceAvailable]).

This method will help you throughout your project in any controller, You have to just import ClsGlobal like #import "ClsGlobal.h".



来源:https://stackoverflow.com/questions/13699256/uiimagepickercontroller-check-camera

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!