views:

423

answers:

1

I'm new to iPhone and I'm using this code to select an image from the iPhone library and show it in imageView. The library is showing images, but when I'm selecting the image, it doesn't appear in the image view..

- (void)viewDidLoad {
    [super viewDidLoad];

 UIImagePickerController * picker = [[UIImagePickerController alloc] init];

 picker.delegate = self;

 picker.sourceType =  UIImagePickerControllerSourceTypePhotoLibrary;

 [self presentModalViewController:picker animated:YES];
}

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

 [picker dismissModalViewControllerAnimated:YES];

 imageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
}

Please can you tell me what's the problem with the code?

A: 

Try with:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *pickedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
    imageView.image = pickedImage;

    [self dismissModalViewControllerAnimated:YES];
}

UIImagePickerControllerOriginalImage is a string constant that could be different from @"UIImagePickerControllerOriginalImage" string. Then, it's safe and more clear to former set the image view, and latter to dismiss the modal view controller.

Pay attention that you should invoke dismissModalViewControllerAnimated: on self because you are dismissing the modal view controller "owned" by self.

Bye!

muccy
thanks for the code ....this code is not working still not getting any image in my imageView
mayank sahai
If you are using Interface Builder check you set the outlet correctly: self.imageView could be nil! ;-)
muccy