Question

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?

Was it helpful?

Solution

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!

OTHER TIPS

I fixed my issue of "grey box appearing instead of an image in UIImageView".

I forgot to make the Background color of the UIIMageView to be CLEAR COLOR. THe problem was solved by setting background color to clearColor. Was using XIB and you should also check if anything is laid out wrong in Interface builder in case you are using interface builder to create UI.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top