UIImagePickerController, Check camera

-(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 warn you when you do not have a camera. Launch the iPhone app and move to this code. But, Crash (This mistake>

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

Why is this?

+4
source share
2 answers

use this code and add the UIImagePickerControllerDelegate delegate to the .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]; } 
+7
source

Create an NSObject class and name it as ClsGlobal or the name you want.

then write +(BOOL)isCameraDeviceAvailable in ClsGlobal.h and execute the following 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 an available camera has not yet been specified.

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

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

+3
source

All Articles