Friday, September 10, 2010

"UIImagePickerController does not work"!


Here is another common mistake that I have seen with UIImagePickerController. Say, I want to display the picked image in the UIImageView (imageView), but it doesn't work. It is a simple issue, can anybody guess what the problem is before you see the answer below?
- (IBAction)takePhoto {

     UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
     imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
     [self presentModalViewController:imagePicker animated:YES];
     [imagePicker release];
}

- (IBAction)choosePhoto {

     UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
     //imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
     [self presentModalViewController:imagePicker animated:YES];
     [imagePicker release];
}

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

     UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
     imageView.image = image;

     [self dismissModalViewControllerAnimated:YES];


ANSWER:
We have not set the delegate! So no call backs being obtained! Just add : 
   
 imagePicker.delegate = self;


2 comments:

  1. Where do you add the line? It's not specified...

    ReplyDelete
  2. Thanks for the comment. You add the line after allocating the UIImagePickerController (in the takePhoto method) before you present it modally. Hope that helps!

    ReplyDelete