nabrugir
nabrugir

Reputation: 1869

Show image from url in uiimageview

I try to show a image in my iphone app but it doesn't show. I connect in IB and I check to show a local image, it workd good. I also check the url from image and it is ok. I use the next code:

NSURL *imageURL = [NSURL URLWithString: aVideo.urlThumbnail];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [UIImage imageWithData:imageData]; 
imageView.image = image;

and

//Video.h
NSString *urlThumbnail;

and this is my code on the view.

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    NSLog(@"URLTHUMBNAIL: %@", aVideo.urlThumbnail); 
    NSURL *imageURL = [NSURL URLWithString: aVideo.urlThumbnail];
    NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
    UIImage *image = [UIImage imageWithData:imageData]; 
    imageView.image = image;
}

NSLog getts on console

URLTHUMBNAIL: http://147.83.74.180/thumbnails/56.jpg

The connections on IB are ok, cause I can display a local image using next code on the same IBAction

UIImage *img = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"barret" ofType:@"png"]];

Upvotes: 2

Views: 5692

Answers (2)

pwc
pwc

Reputation: 7103

First off, since NSData dataWithContentsOfURL: accesses the network, you should consider doing that in the background. Otherwise, your UI may become unresponsive.

You should consider adding some additional logging to check the value of imageData and image. Also, you can call NSData dataWithContentsOfURL:options:error: to see if that's the source of the failure.

Upvotes: 0

Dan Ray
Dan Ray

Reputation: 21893

It can be very helpful to NSLog() yourself some notes about what's really in your variables. If you really do have everything hooked up right in IB, it could be that aVideo.urlThumbnail isn't getting set. I'd like to see that value in the console right before creating your NSURL with it.

Upvotes: 1

Related Questions