Reputation: 8472
I can't understand why I can't set the uimageview from url from the example below:
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://a4.mzstatic.com/us/r30/Music/08/e9/14/mzi.lmcnwrii.60x60-50.jpg"]];
UIImage *image = [UIImage imageWithData:data];
self.imgAlbum1 = [[UIImageView alloc] initWithImage:image];
it works fine by browsing the image, IBOutlet is ok, even setting uiimage from imagenamed doesn't work
I tried to recreate the contorl in the storyboard but nothing...
any tips are appreciated
Upvotes: 0
Views: 1127
Reputation: 1546
Edit: If your imgAlbum1
is an IBOutlet
, you probably set it as a WEAK property. If that is the case, the image will never show up. In order to use alloc
, your property would have to be a STRONG property.
Since you are probably using a WEAK outlet, try using self.imgAlbum1
as the other poster suggested. The reason this will work though, is because you're just updating the image, not the actual imageView
object (weak properties, when you re-alloc will essentially destroy the object completely).
This is what I did which I tested and works just fine:
NSString *encodedString = [[NSString stringWithFormat:@"%@", @"us/r30/Music/08/e9/14/mzi.lmcnwrii.60x60-50.jpg"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *imageURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://a4.mzstatic.com/%@", encodedString]];
NSData *data = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [[UIImage alloc] initWithData:data];
UIImageView *imgAlbum1 = [[UIImageView alloc] initWithImage:image];
[self.view addSubview:imgAlbum1];
So the questions for me are:
@property (nonatomic, weak) IBOutlet *imgAlbum1)
? If so, don't set it to strong - rather, just use self.imgAlbum1.image = image;
imgAlbum1
an IBOutlet
to a UIImageView
? If so, is it hidden (or hidden by a layer)?etc.
Otherwise, the code seems fine.
Upvotes: 1
Reputation: 17218
Presumably imgAlbum1
is an outlet to your existing image view.
Instead of that last line, which tries to create a new image view without displaying it anywhere, you probably want:
self.imgAlbum1.image = image;
Upvotes: 2