UITableViewCell图像没有加载ios

时间:2013-08-03 03:22:57

标签: ios asynchronous uiimage nsurl

我正在尝试异步下载图像并在UITableViewCell imageView中显示它们。问题是我的NSURL由于某种原因是零的。

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

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

这将打印类似

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

有人可以告诉我为什么会这样吗?感谢。

1 个答案:

答案 0 :(得分:0)

您正在做的是使用NSURL在网址中获取内容。一旦你有了url的内容,你基本上是对编译器说,从这个url加载这些SOMETHING内容。但是,无论是图像还是json对象,SOMETHING都需要加载到NSData对象中,然后将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:)

相关问题