Rory Lester
Rory Lester

Reputation: 2918

Issue with displaying UIImageView

I am trying to display an UIImageView using [self.view addSubview:imageView]; however nothing happens. imageView is not empty. I assign an an image from image library using the following code ;

-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSString *mediaType = [info
                           objectForKey:UIImagePickerControllerMediaType];
    [self dismissModalViewControllerAnimated:YES];
    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
        UIImage *image = [info 
                          objectForKey:UIImagePickerControllerOriginalImage];

        [imageView setImage:image];
        if (newMedia)
            UIImageWriteToSavedPhotosAlbum(image, 
                                           self,
                                           @selector(image:finishedSavingWithError:contextInfo:),
                                           nil);
    }
}

Why does it not work?

imageView is created as follows ;

UIImageView *imageView;
@property (nonatomic, retain) IBOutlet UIImageView *imageView;
@synthesize imageView;

Upvotes: 0

Views: 157

Answers (1)

sch
sch

Reputation: 27506

Declaring and synthesizing the property imageView does not create an instance of UIImageView. So, imageView is nil in the following line:

[imageView setImage:image];

You have to create an instance of UIImageView at some point before using it. For example:

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

    self.imageView = [[UIImageView alloc] initWithFrame:(CGRectMake(100, 100, 100, 100)];
    self.imageView.image = image;
    [self.view addSubview:self.imageView];

    //...
}

Or, you probably forgot to link to imageView from Interface Builder if you are using it.

Upvotes: 1

Related Questions