Show image from url in uiimageview

I am trying to show an image in my iphone application but it is not showing. I am connecting in IB and I am checking to show the local image, this is good. I also check the url from the image and everything is fine. I am using the following 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 in 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

      

Connections on IB are ok because I can display a local image using the following code on the same IBAction

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

      

+2


a source to share


2 answers


This can be very helpful for NSLog()

taking notes on what is really in your variables. If you really have everything IB related, it may aVideo.urlThumbnail

not be installed. I would like to see this value in the console right before creating your NSURL.



+1


a source


First of all, since it is NSData dataWithContentsOfURL:

accessing the network, you should think about it in the background. Otherwise, your user interface may become unresponsive.



You should consider adding an additional log to validate the imageData

and values image

. Alternatively, you can call NSData dataWithContentsOfURL:options:error:

to find out if the source is a failure.

0


a source







All Articles