在 UITableView 中延迟加载图像 [英] Lazy load images in UITableView

查看:34
本文介绍了在 UITableView 中延迟加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 UITableView 中有大约 50 个自定义单元格.我想在从 URL 获取图像的单元格中显示图像和标签.

I have some 50 custom cells in my UITableView. I want to display an image and a label in the cells where I get the images from URLs.

我想延迟加载图像,以便在加载图像时 UI 不会冻结.我尝试在单独的线程中获取图像,但每次单元格再次可见时我都必须加载每个图像(否则重用单元格会显示旧图像).有人可以告诉我如何复制这种行为.

I want to do a lazy load of images so the UI does not freeze up while the images are being loaded. I tried getting the images in separate threads but I have to load each image every time a cell becomes visible again (Otherwise reuse of cells shows old images). Can someone please tell me how to duplicate this behavior.

推荐答案

试试AFNetworking类 在这个链接下载这个类https://github.com/AFNetworking/AFNetworking.在您的项目中添加所有 AFNetworking 类.然后只需导入此类别

Try AFNetworking class download this class in this link https://github.com/AFNetworking/AFNetworking . Add the all AFNetworking class in your project.Then just import this category

#import "UIImageView+AFNetworking.h" in your Viewcontroller which contains your Tableview.

然后在 cellForRowAtIndexPath: 中放如下

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    if (cell == nil) 
    {
        cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }

    [cell.imageView setImageWithURL:[NSURL URLWithString:[UrlArray objectAtIndex:indexPath.row]] placeholderImage:[UIImage imageNamed:@"placeholder.jpg"]];
     cell.myLabel.text = [imageNameArray objectAtIndex:indexPath.row];

     cell.selectionStyle = UITableViewCellSelectionStyleNone;

    return cell;    
}

这会异步下载 UITableviewcell 中的 imageView 的图像.当用户滚动 Tableview 时,它不会一次又一次地下载,因为它也有缓存.下载图像后,它会使用 imageUrl 的密钥保存图像.希望对你有用.

This download your Image for imageView in the UITableviewcell asynchronously. It wont download again and again while the user scroll the Tableview, because it has cache also. Once your image download it save the image with key of your imageUrl. I hope it useful for you.

这篇关于在 UITableView 中延迟加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆