2013-08-03 94 views
0

我想异步下载图像并将它们显示在UITableViewCell imageView中。问题是,我的NSURL为零出于某种原因..UITableViewCell图像不加载ios

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

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

这将打印出类似这样

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

有人能告诉我为什么发生这种情况?谢谢。

+0

确实[这](HTTP:// stackoverflow.com/questions/1981390/urlwithstring-returns-nil)有帮助吗? – Shinigami

+0

耶,工作..谢谢 –

+0

哦,我以为你有问题从NSURL到UIImage ...我想我误解了你的问题:( – jsetting32

回答

0

你在做什么是得到一个url内的内容NSURL。一旦你有了url的内容,你基本上就会对编译器说,从这个URL加载这些SOMETHING内容。但是,要知道它是图像还是json对象或其他东西,需要加载到对象中,然后将NSData对象加载到UIImage对象中。现在,你只能说cell.imageView.image = theUIimage为您的具体问题,但继承人只是加载这样UIImage成一个简单的例子一个UIImageView像这样:

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]; 

请没有downvote :)

+0

+ 1为“请不要倒票”我也讨厌倒票: ) –

+0

罚款没有倒票,但你从url下载图像的方式,它会阻止主线程,因为你正在使用同步连接。 – Exploring