
iOS tableView的图片缓存异步加载
发布日期:2021-05-24 08:24:27
浏览次数:18
分类:精选文章
本文共 6783 字,大约阅读时间需要 22 分钟。
建立UIScrollView和UITableView的ViewController
1. 创建UIViewController类
首先,我们需要创建一个UIViewController类,并将其设置为UIScrollView和UITableView的代理。
#import@protocol ICTableViewDelegate -(void) cellImageDidLoad:(NSIndexPath *)indexPath image:(NSMutableArray *)imageArray; @end @interface ICTableViewController : UIViewController @property (nonatomic, strong) id delegate; @property (nonatomic, strong):UITableView *tableView; @property (nonatomic, strong) NSMutableArray *tableDataArray; @end
2. 实现图片缓存的逻辑
我们需要在.loadCellImage方法中实现图片缓存和加载的逻辑。这将确保图片能够在视图中展示,同时避免重复请求
-(void) loadCellImage { NSArray *indexPathsForLoad = [self.tableView indexPathsForVisibleRows]; for (NSIndexPath *item in indexPathsForLoad) { NSInteger rowNumberForCell = item.row; if (rowNumberForCell >= [self.tableDataArray count]) { return; } NSString *imageStr = self.tableDataArray[rowNumberForCell][@"photoFileUrl"]; if ([imageStr length] == 0) { continue; } NSArray *photoUrl = [imageStr componentsSeparatedByString:MULTI_FILES_SEPARATOR]; for (NSInteger i = 0; i < photoUrl.count; i++) { NSString *url = [photoUrl[i] stringByAppendingString:@".temp"]; NSString *imageDataPath = [NSHomeDirectory() stringByAppendingPathComponent:[ @"Library/Caches/" stringByAppendingPathComponent:url ]]; NSData *imageData = [NSData dataWithContentsOfFile:imageDataPath]; if (imageData) { UIImage *image = [UIImage imageWithData:imageData]; if (!image) { continue; } UIImage *thumbImg = [FileTransferHelp thumbnailWithImage:image size:(CGSizeMake(60, 40))]; if (thumbImg) { //设置图片到相应的控件中 [photoBtn.imgView setImage:thumbImg]; [cell.contentView addSubview:photoBtn]; } } } } }
3. 子类化UIViewController并实现代理方法
在子类化UIViewController中,设置单元格内容并实现ICTableViewDelegate代理方法
@interface CustomPhotoTableViewCell : UITableViewCell @property (nonatomic, strong) IBOutlet UIButton *photoBtn; @property (nonatomic, strong) UILabel *urlLabel; @property (nonatomic, strong) UIImageView *imgView; @end @interface CustomPhotoTableViewController : UIViewController@property (nonatomic, strong) UITableView *tableView; @property (nonatomic, strong) NSMutableArray *tableDataArray; @end @implementation CustomPhotoTableViewController -(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { CustomPhotoTableViewCell *cell = (CustomPhotoTableViewCell *)[tableView dequeueReusableCellWithIdentifier: [UITableViewCell 蒂PropertyValue:nil]]; if (!cell) { [tableView dequeueReusableCellWithIdentifier:@"CustomPhotoTableViewCell"]; } NSString *imageName = [url stringByAppendingString:@".temp"]; NSString *imageDataPath = [NSHomeDirectory() stringByAppendingPathComponent:[ @"Library/Caches/" stringByAppendingPathComponent:imageName ]]; UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfFile:imageDataPath]]; UIImage *thumbImg = [FileTransferHelp thumbnailWithImage:image size:(CGSizeMake(60, 40))]; if (thumbImg) { [cell.imgView setImage:thumbImg]; } else { [cell.imgView setImage:[UIImage imageNamed:@"placeholder"]]; } [cell.photoBtn/fileFullName setText:url]; return cell; } -(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // 单元格点击事件处理 } - (void) cellImageDidLoad:(NSIndexPath *)indexPath image:(NSMutableArray *)imageArray { CustomPhotoTableViewCell *cell = (CustomPhotoTableViewCell *)[tableView cellForRowAtIndexPath:indexPath]; if (imageArray.count > 0) { for (NSInteger i = 0; i < imageArray.count; i++) { if (is_iOS8_or_later) { CustomPhotoBtn *photoBtn = (CustomPhotoBtn *)[cell.contentView viewWithTag:(i + 1) * 10]; if (photoBtn) { UIImage *thumbImg = [FileTransferHelp thumbnailWithImage:(UIImage *)imageArray[i] size:(CGSizeMake(60, 40))]; [photoBtn.imgView setImage:thumbImg]; [cell.contentView addSubview:photoBtn]; } else { [tableView beginUpdates]; [cell(Layout setFrame:cell(Layout).frame); [tableView endUpdates]; } } else { CustomPhotoBtn *photoBtn = (CustomPhotoBtn *)[cell.contentView viewWithTag:(i + 1) * 10]; if (photoBtn) { UIImage *thumbImg = [FileTransferHelp thumbnailWithImage:(UIImage *)imageArray[i] size:(CGSizeMake(60, 40))]; [photoBtn.imgView setImage:thumbImg]; [cell.contentView addSubview:photoBtn]; } else { [tableView beginUpdates]; [cell(Layout setFrame:cell(Layout).frame); [tableView endUpdates]; } } } } }
4. 设置单元格内容
在cellForRowAtIndexPath中设置单元格内容
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomPhotoTableViewCell"]; if (!cell) { cell = [tableView dequeueReusableCellWithIdentifier:@"CustomPhotoTableViewCell"]; } CustomPhotoBtn *photoBtn = [CustomPhotoBtn customPhotoBtn]; photoBtn.fileFullName.textColor = [UIColor blueColor]; NSString *imageName = [url stringByAppendingString:@".temp"]; NSString *imageDataPath = [NSHomeDirectory() stringByAppendingPathComponent:[ @"Library/Caches/" stringByAppendingString:imageName ]]; NSData *imageData = [NSData dataWithContentsOfFile:imageDataPath]; if (imageData) { UIImage *image = [UIImage imageWithData:imageData]; if (image) { UIImage *thumbImg = [FileTransferHelp thumbnailWithImage:image size:(CGSizeMake(60, 40))]; if (thumbImg) { [photoBtn.imgView setImage:thumbImg]; [cell.contentView addSubview:photoBtn]; } else { [cell.imgView setImage:[UIImage imageNamed:@"placeholder"]]; } } else { [cell.imgView setImage:[UIImage imageNamed:@"placeholder"]]; } } else { [cell.imgView setImage:[UIImage imageNamed:@"placeholder"]]; }[cell.contentView addSubview:photoBtn]; return cell;
5. 最后运行代码
确保项目中正确引入必要的头文件,并确保所有必要的控件已经在xib文件中定义。运行项目测试表格是否正确加载图片,如果图片缓存存在,直接显示;否则按需求加载和缓存。
通过这些步骤,您可以轻松实现带有UIScrollView和UITableView的缓存图片加载功能,同时确保性能优化和用户体验良好。
发表评论
最新留言
表示我来过!
[***.240.166.169]2025年04月16日 12时12分22秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
LINUX-WIFI无线接入的一些东西
2019-03-15
word文档手写字母总会大写问题
2019-03-15
Redis中的key
2019-03-15
juc-09-控制并发流程工具类
2019-03-15
第一节 docker安装
2019-03-15
Spring 和 DI 依赖注入
2019-03-15
中序线索二叉树的遍历
2019-03-15
laravel server error 服务器内部错误
2019-03-15
Linux驱动实现GPIO模拟I2C读写操作
2019-03-15
iJ配置Maven环境详解
2019-03-15
仿QQ登陆界面
2019-03-15
N皇后问题解法(递归+回朔)
2019-03-15
面试题 08.01. 三步问题
2019-03-15
剑指 Offer 11. 旋转数组的最小数字
2019-03-15
word文档注入(追踪word文档)未完
2019-03-15
作为我的第一篇csdn博客吧
2019-03-15
ajax异步提交失败
2019-03-15
一道简单的访问越界、栈溢出pwn解题记录
2019-03-15
测试调用另一台电脑ip是否有用
2019-03-15
mos-excel集成文档
2019-03-15