Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use the UIImagePickerController on a iphone simulator

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?

like image 387
Eugene Trapeznikov Avatar asked Feb 07 '12 08:02

Eugene Trapeznikov


3 Answers

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.

like image 163
Krrish Avatar answered Nov 03 '22 04:11

Krrish


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.

like image 24
KlimczakM Avatar answered Nov 03 '22 02:11

KlimczakM


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];
        }
like image 25
Meera Avatar answered Nov 03 '22 02:11

Meera