1

I have an app that needs to take a picture and then show it to the user. I opened the camera to let the user take a picture using the following code:

if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    imagePicker.sourceType =
    UIImagePickerControllerSourceTypeCamera;
    imagePicker.mediaTypes = @[(NSString *) kUTTypeImage];
    imagePicker.allowsEditing = NO;
    imagePicker.modalPresentationStyle = UIModalPresentationFullScreen;

    [self presentViewController:imagePicker animated:YES completion:nil];
} else {
    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle: @"Camera failed to open"
                          message: @"Camera is not available"
                          delegate: nil
                          cancelButtonTitle:@"OK"
                          otherButtonTitles:nil];
    [alert show];
}

This present the camera view so the user can take a picture. I implemented the methods to handle the cancel and complete events like this:

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    [self dismissViewControllerAnimated:YES completion:nil];

    UIImage * image = [info objectForKey:UIImagePickerControllerEditedImage];
    [photoView setImage:image];
    [photoView setNeedsDisplay];
}

The problem is that after going through the last function, image is nil. At first dismissViewControllerAnimated was at the end of the function, but in this SO post the answer explains that dismissViewControllerAnimated should be called first and picker should be released before getting the image from info. I tried putting dismissViewControllerAnimated at the top, but because I'm using ARC I cannot release picker.

How can I get the image from info?

Community
  • 1
  • 1
Kevin
  • 2,739
  • 33
  • 57

3 Answers3

1

try this:

UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
// or UIImage *image = [info objectForKey:@"UIImagePickerControllerEditedImage"];
thorb65
  • 2,696
  • 2
  • 27
  • 37
1

You have set picker.allowsEditing = NO; in your code. Then why are you retrieving the edited image? It won't exist. Anyways, if you allow editing, use this:

photoView.image = info[UIImagePickerControllerEditedImage] ? info[UIImagePickerControllerEditedImage] : info[UIImagePickerControllerOriginalImage]

If the user has edited the photo, photoView.image will be set to the edited photo, otherwise it will be set to the original photo.

If you don't allow editing, simply retrieve the image using the UIImagePickerControllerOriginalImage key.

duci9y
  • 4,128
  • 3
  • 26
  • 42
-1

Check in this method,

 (void) imagePickerController:(UIImagePickerController *)_picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
      NSLog(@"dictionary image info: %@",info );
    }

In this method, print the dictionary and check that are you getting UIImagePickerControllerMediaType and UIImagePickerControllerOriginalImage. (In my case, I m getting value like this UIImagePickerControllerMediaType = "public.image"; UIImagePickerControllerOriginalImage = UIImage: 0x1dd79520";)

Now if you are getting value for UIImagePickerControllerOriginalImage then you save value in this UIImage.

Now use this.

UIImage *image = [info objectForKey:@"UIImagePickerControllerEditedImage"];

OR

UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

Hope this will help you.

Nirav Jain
  • 5,088
  • 5
  • 40
  • 61