【问题标题】:UITableViewCell images not loading iosUITableViewCell 图像未加载 ios
【发布时间】:2013-08-04 10:28:14
【问题描述】:

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

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

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

这将打印类似

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

谁能告诉我为什么会这样?谢谢。

【问题讨论】:

  • this 有帮助吗?
  • 是的,工作......谢谢
  • 哦,我以为您在从 NSURL 到 UIImage 时遇到问题...我想我误解了您的问题 :(

标签: ios asynchronous uiimage nsurl


【解决方案1】:

您所做的是使用NSURL 获取网址中的内容。一旦你有了 url 的内容,你基本上就是在对编译器说,从这个 url 加载这些 SOMETHING 内容。但是,无论是图像还是 json 对象或其他任何东西,都需要将其加载到 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];

请不要投反对票:)

【讨论】:

  • 没有反对票很好,但是您从 url 下载图像的方式会阻塞主线程,因为您使用的是同步连接。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-06
  • 2017-06-04
  • 1970-01-01
  • 1970-01-01
  • 2021-10-25
相关资源
最近更新 更多