Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIImageView not showing Image

I have the following code:

@interface NeighborProfileViewController : UIViewController {
    UIImageView * profPic;
    UITextView * text;
    UIButton * buzz;
    NSString * uid;
    NSMutableData *responseData;
    NSDictionary * results;
}

@property (nonatomic, retain) IBOutlet UIImageView * profPic;
@property (nonatomic, retain) IBOutlet UITextView * text;
@property (nonatomic, retain) IBOutlet UIButton * buzz;
@property (nonatomic, retain) NSString * uid;

@end

Here's what I have in the viewDidLoad on the .m file

@implementation NeighborProfileViewController
@synthesize uid;
@synthesize profPic;
@synthesize buzz;
@synthesize text;


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
    NSURL *url = //some URL to the picture;
    NSData *data = [NSData dataWithContentsOfURL:url];
    UIImage *img = [[[UIImage alloc] initWithData:data] autorelease];
    self.profPic = [[UIImageView alloc] initWithImage: img];
}

I think I wired up the UIImageView via IB correctly. I have an outlet from the UIImageView to the profPic in the Files Owner. What am I doing wrong?

like image 862
aherlambang Avatar asked Jan 31 '11 04:01

aherlambang


2 Answers

If you are setting a default image is that staying visible after you call self.profPic = [UIImageView] or is it being removed from the screen? I think this problem comes when self.profPic releases the old image view to replace it with the one you've just created. The old UIImageView instance, along with all the properties which you defined in IB, are probably automatically removed from the superview. Which is why you're not seeing the downloaded image.

If you used IB to create the UIImageView then you don't want to create a new ImageView and assign it to profPic (which is already a fully instantiated UIImageView). Try calling [profPic setImage:img]; which will just change the image in the profPic imageview.

like image 118
thelaws Avatar answered Sep 18 '22 17:09

thelaws


@Equinox use

  [profPic setImage:img];

instead of

self.profPic = [[UIImageView alloc] initWithImage: img];

although using autorelease is not an issue here. you can also do something like this

  UIImage *img = [[UIImage alloc] initWithData:data] ;
  [profPic setImage:img];
  [img release];

the problem with

self.profPic = [[UIImageView alloc] initWithImage: img];

is that you are now creating another memory location for profPic and that means profPic will no longer point to the UIImageView in your IB any more

like image 22
Robin Avatar answered Sep 20 '22 17:09

Robin