多线程实现多图片下载1
2016-03-27 14:54
141 查看
展示效果如下:
View Code
这两个方法是UITableView必须要实现的方法
第一个是返回数据量,没什么好说的
第二个是绑定数据
具体的流程看下图
#pragma mark - 数据源方法 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.apps.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *ID = @"app"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; DDZApp *app = self.apps[indexPath.row]; cell.textLabel.text = app.name; cell.detailTextLabel.text = app.download; //先从内存中取出图片 UIImage *image = self.imgCache[app.icon]; if (image) { cell.imageView.image = image; }else { //内存中没有图片 //将图片文件数据写入到沙盒中 NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]; //获得文件名 NSString *filename = [app.icon lastPathComponent]; //计算出文件的全路径 NSString *file = [cachesPath stringByAppendingPathComponent:filename]; //加载沙盒的文件数据 NSData *data = [NSData dataWithContentsOfFile:file]; //判断沙盒中是否有图片 if (data) { //直接加载沙盒中图片 cell.imageView.image = [UIImage imageWithData:data]; //存到字典(内存)中 self.imgCache[app.icon] = cell.imageView.image; }else { //下载图片 data = [NSData dataWithContentsOfURL:[NSURL URLWithString:app.icon]]; cell.imageView.image = [UIImage imageWithData:data]; //存到内存中 self.imgCache[app.icon] = cell.imageView.image; //将图片数据写入到沙盒中 [data writeToFile:file atomically:YES]; } } return cell; }
View Code
这两个方法是UITableView必须要实现的方法
第一个是返回数据量,没什么好说的
第二个是绑定数据
具体的流程看下图
相关文章推荐
- hdu 3371(kruskal)
- HDU 5651 逆元
- VC++6.0 unexpected end of file while looking for precompiled header directive
- 安卓手机屏幕基础解析
- Java可变参数方法的重载实例
- VS错误error C1083 和XX.pch错误
- C#全局键盘检测GetAsyncKeyState
- 操作系统内存页面置换算法
- Scrum 之 四大支柱和价值观
- 日志分析项目
- 成绩排名——华为上机笔试题
- 购物商城shopping连载(1)
- form表单提交之前推断
- Linux CentOS6系统安装最新版本Node.js环境及相关文件配置
- 面试感悟----一名3年工作经验的程序员应该具备的技能
- RoaringBitmap简析
- Linux 上的原生 ZFS 支持已功能完备,支持 2.6.32 到 4.5 的 Linux 内核
- C#-双问号运算符
- 简洁版OPenCV2.4.7+VS2012 Debug relase 属性单配置并保存
- 第五周项目一 三角形类雏形(1)