【问题标题】:Images from url in UITableViewCell loads only when i scroll the table view来自 UITableViewCell 中 url 的图像仅在我滚动表格视图时加载
【发布时间】:2014-09-27 06:46:35
【问题描述】:

仅当我开始滚动 UITableView 时,从 url 下载的图像才会在 UITableViewCell 中显示图像。现在我正在使用以下代码从 url 加载图像:

NSURL* url = [NSURL URLWithString:imageURL];
    NSURLRequest* request = [NSURLRequest requestWithURL:url];


[NSURLConnection sendAsynchronousRequest:request
        queue:[NSOperationQueue mainQueue]
        completionHandler:^(NSURLResponse * response,
            NSData * data,
            NSError * error) {
    if (!error){
            NSImage* image = [[NSImage alloc] initWithData:data];
        // do whatever you want with image
    }

}];

提前致谢。

【问题讨论】:

  • 您从哪里调用此代码?你能添加你的 tableView 更新方法吗?
  • 我在单元格中使用此代码作为 indexpath 的行。

标签: objective-c uitableview uiimage nsurlrequest


【解决方案1】:

您不应该在- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath 中获取您的图像,您应该创建一个实用程序类作为支持数据存储。正如您现在的代码所示,您可以想象在单元格滚动进出视图时多次重新请求图像。

【讨论】:

    【解决方案2】:

    强烈推荐SDWebImage,非常简单,功能强大。

    您可以使用setImageWithURL category ,只需为imageView设置图像。例如

    [cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
                   placeholderImage:[UIImage imageNamed:@"placeholder.png"]];`
    

    您可以通过[[SDImageCache sharedImageCache] storeImage:myImage forKey:myImageCacheKey] 保存您的图像缓存,并通过[[SDImageCache sharedImageCache] queryDiskCacheForKey:myImageCacheKey done:^doneBlock] 查询您的图像缓存;

    非常简单且功能强大,更多详情请查看此处SDWebImage

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多