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?
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.
@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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With