0

I am trying to download images asynchronously and display them in UITableViewCell imageView. The problem is that my NSURL is nil for some reason..

        NSString *urlString = feed[@"imageURL"];
        NSLog(@"urlString %@", urlString);

        NSURL *url = [NSURL URLWithString:urlString];            
        NSLog(@"url image %@", url);

This will print something like

urlString http://www.....image.jpg
url image (null)

Can someone please tell me why this is happening? Thanks.

Tim Tuffley
  • 605
  • 1
  • 6
  • 20

1 Answers1

0

What your doing is getting the contents within a url with NSURL. Once you have the contents of the url, you are basically saying to the compiler, load these SOMETHING contents from this url. But that SOMETHING whether it's an image or json object or whatever, needs to be loaded into an NSData object then load the NSData object into a UIImage object. Now you can just say cell.imageView.image = theUIimage for your exact problem but heres a simple example that just loads such UIImage into a UIImageView like so:

NSURL *URL = [NSURL URLWithString:@"http://l.yimg.com/a/i/us/we/52/37.gif"];
NSData *data = [NSData dataWithContentsOfURL:URL];
UIImage *img = [[UIImage alloc] initWithData:data];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
imageView.image = img;
[self.view addSubview:imageView];

Please no downvote :)

jsetting32
  • 1,632
  • 2
  • 20
  • 45
  • Fine for no down votes, but the way you are downloading image from url it will block main thread because you are using synchronous connection. – Exploring Aug 07 '13 at 06:11