UIImageView won't display

Discussion in 'iPhone/iPad Programming' started by Slocum, Feb 7, 2010.

  1. macrumors newbie

    Joined:
    Feb 7, 2010
    #1
    I can get UIImageView to display the image, if I set the image in the Interface Builder. But nothing shows when I set the parameters programmatically.

    Here is the code:

    ImageTestViewController.h:

    Code:
    #import <UIKit/UIKit.h>
    
    @interface ImageTestViewController : UIViewController {
    	UIImageView *imageView;
    }
    @property (nonatomic, retain) IBOutlet UIImageView *imageView;
    @end
    ImageTestViewController.m:

    Code:
    #import "ImageTestViewController.h"
    
    @implementation ImageTestViewController
    
    @synthesize imageView;
    
    - (void)viewDidLoad {
    	UIImage *image = [UIImage imageNamed:@"apress_logo.png"];
    	imageView = [[UIImageView alloc] initWithImage:image];
        [super viewDidLoad];
    }
    
    - (void)viewDidUnload {
    	imageView = nil;
    	[super viewDidUnload];
    }
    
    - (void)dealloc {
    	[imageView release];
        [super dealloc];
    }
    
    @end
    
    I connected the UIImageView in the interface to the IBOutlet. The image is in the Resources. I still won't display. What have I done wrong?

    Thanks.
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    I am confused. If you have connected your IBOutlet to a UIImageView instance in Interface Builder then why are you creating a new UIImageView in your init? You are not altering the properties of the UIImageView you created in IB by doing this. You should be setting the image property of the instance you already have.

    If you want to create your own UIImageView (if you do this delete then one in IB) then you should be adding it to a visible view hierarchy. The UIImageView you create is never added to a view that's on a visible window so will never be seen.
     
  3. thread starter macrumors newbie

    Joined:
    Feb 7, 2010
    #3
    Thank you. Boneheaded mistake.

    I've changed it to:

    Code:
    - (void)viewDidLoad {
    	UIImage *image = [UIImage imageNamed:@"apress_logo.png"];
    	imageView.image = image;
        [super viewDidLoad];
    }
    and it works.
     

Share This Page