开发者

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 th开发者_开发问答ats 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?


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.


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)
}


Put Below Code Where Exception Occures. Remember You Need To Implement navigationController

 if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"ERROR" message:@"No Camera Avalible" preferredStyle:UIAlertControllerStyleAlert];

        UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
            [self dismissViewControllerAnimated:alertView completion:nil];
        }];
        [alertView addAction:ok];
        [self.navigationController presentViewController:alertView animated:YES completion:nil];
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜