Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uiimagepicker For showing camera

Hi everyone I am trying to make a camera app. I am doing this as

picker.sourceType = UIImagePickerControllerSourceTypeCamera;

where picker is the object of UIimagepicker Controller.

But when is run the code then the application terminates showing the error.

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Source type 1 not available'

I am using this on simulator. I know that it is not possible to check camera in simulator, but we can test for that. I think it might be that because camera is not available thats why it's terminating. But I saw an application with the same code but that was running on the simulator, just showing the camera view. Just help me out how to resolve this problem. And moreover how can I put my custom view to the camera in that app?

like image 867
Sabby Avatar asked Nov 03 '10 10:11

Sabby


2 Answers

You need to check if the device has camera available before setting the sourcetype.

The following can check if device has camera available.

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])  {
}

You can't check the camera functionality from your simulator. You can assign UIImagePickerControllerSourceTypePhotoLibrary as the sourceType to test on simulator.

like image 200
Chaitanya Avatar answered Oct 19 '22 09:10

Chaitanya


Swift 2.2

if UIImagePickerController.isSourceTypeAvailable(.Camera) {
  imagePicker.delegate = self
  imagePicker.sourceType = .Camera
  presentViewController(imagePicker, animated: true, completion: nil)
} else {
  print("The device has no camera")
}

Saved photos album

if UIImagePickerController.isSourceTypeAvailable(.SavedPhotosAlbum) {
  imagePicker.delegate = self
  imagePicker.sourceType = .SavedPhotosAlbum
  imagePicker.allowsEditing = false
  self.presentViewController(imagePicker, animated: true, completion: nil)
}
like image 45
Antoine Floury Avatar answered Oct 19 '22 11:10

Antoine Floury