6

I have the method, that take photos from gallery or from the camera

-(IBAction) getPhoto:(id) sender {
  UIImagePickerController * picker = [[UIImagePickerController alloc] init];
  picker.delegate = self;

  if((UIButton *) sender == choosePhotoBtn) {
        picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
  } else {
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
  }

  [self presentModalViewController:picker animated:YES];
}

But when i run it on the simulator, code doesnt work. And it doesnt work in picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum and picker.sourceType = UIImagePickerControllerSourceTypeCamera

Is the problem in the simulator or in the code?

Eugene Trapeznikov
  • 3,220
  • 6
  • 47
  • 74

4 Answers4

9

Try this,

 if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
        {
            picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        }
        else
        {
            picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        }
        [self.navigationController presentModalViewController:picker animated:NO];

If you are creating the app for iPad. You will have to present the gallery in a popOver control.

Krrish
  • 2,256
  • 18
  • 21
  • [self.navigationController presentModalViewController:picker animated:NO] - doesnt work, but [self presentModalViewController:picker animated:YES] is ok. and about sourceType i got the same question. why UIImagePickerControllerSourceTypePhotoLibrary is empty? how should i add photos before running app – Eugene Trapeznikov Feb 07 '12 at 08:50
2

Swift 3/4/5 verison:

if UIImagePickerController.isSourceTypeAvailable(.camera) {
    picker.sourceType = .camera
}
else {
    picker.sourceType = .savedPhotosAlbum // or .photoLibrary
}

Swift 2 version:

if UIImagePickerController.isSourceTypeAvailable(.Camera) {
    picker.sourceType = .Camera
}
else {
    picker.sourceType = .SavedPhotosAlbum // or .PhotoLibrary
}

In simulator, you can't use cameraCaptureMode and showsCameraControls.

KlimczakM
  • 12,576
  • 11
  • 64
  • 83
1

In simulator your picker.sourceType = UIImagePickerControllerSourceTypeCamera wont be called as there is no camera available in simulator. Also its a good practice to check whether the source type is available to avoid crashes.

#import <MobileCoreServices/UTCoreTypes.h>
….
 if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
        {
            UIImagePickerController *imagePickerCamera =[[UIImagePickerController alloc] init];
            imagePickerCamera.delegate = self;
            imagePickerCamera.mediaTypes = [NSArray arrayWithObjects:(NSString *) kUTTypeImage,nil];
            imagePickerCamera.allowsEditing = YES;
            imagePickerCamera.sourceType = UIImagePickerControllerSourceTypeCamera;

            [self presentViewController:imagePickerCamera  animated:YES completion:nil];
        }

    else if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])
        {
            UIImagePickerController *imagePickerAlbum =[[UIImagePickerController alloc] init];
            imagePickerAlbum.delegate = self;
            imagePickerAlbum.mediaTypes = [NSArray arrayWithObjects:(NSString *) kUTTypeImage,nil];
            imagePickerAlbum.allowsEditing = YES;
            imagePickerAlbum.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

            [self presentViewController:imagePickerAlbum animated:YES completion:nil];
        }
Meera
  • 1,031
  • 6
  • 25
  • Check this link - https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIImagePickerController_Class/UIImagePickerController_Class.pdf – Meera Jan 24 '13 at 12:24
0

Similarly to the above answers, but I found this easier. Show a pop up alert if the device doesn't have a camera (like the simulator). Sam code, different usage:

//button if to take a photo
- (IBAction)takePhoto:(id)sender {

//checks if device has a camera
    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        UIAlertView *noCameraAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"You don't have a camera for this device" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

        //shows above alert if there's no camera
        [noCameraAlert show];
    }

    //otherwise, show a modal for taking a photo
    else {
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
        imagePicker.delegate = self;
        imagePicker.allowsEditing = YES;
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;

        [self presentViewController:imagePicker animated:YES completion:NULL];
    }
}
Elijah Murray
  • 2,132
  • 5
  • 30
  • 43