tableview 图片缓存
需要对图片进行缓存 写入沙盒中,使用多线程下载图将队列放到字典中,value 线程 key 下载路径 判断是否有该key 没有创建添加一个线程下载
赋值图片只需刷新对应cell 就可以
@property(nonatomic,strong)NSMutableDictionary * imageCache;
@property(nonatomic,strong)NSMutableDictionary * operations;
@property(nonatomic,strong)NSOperationQueue * queue;
-(NSOperationQueue *)queue
{
if (!_queue) {
_queue=[[NSOperationQueue alloc]init];
}
return _queue;
}
-(NSMutableDictionary *)operations
{
if (!_operations) {
_operations=[NSMutableDictionary dictionary];
}
return _operations;
}
-(NSMutableDictionary *)imageCache{
if (!_imageCache) {
_imageCache=[NSMutableDictionary dictionary];
}
return _imageCache;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// CarTableViewCell *cell=[[CarTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"int"];
static NSString* ID=@"app";
CarTableViewCell * cell=[tableView dequeueReusableCellWithIdentifier:ID];
if (!cell) {
cell=[[CarTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
}
if (allDataArray.count>0) {
NSDictionary * unit=[allDataArray objectAtIndex:indexPath.row];
cell.titleName.text=[NSString stringWithFormat:@"%@ %@",[unit objectForKey:@"carcName"],[unit objectForKey:@"carkName"]];
cell.timeAndDistance.text=[NSString stringWithFormat:@"%@/%@公里数",[unit objectForKey:@"inspectionDate"],[unit objectForKey:@"mileage"]];
cell.money.text=[NSString stringWithFormat:@"%@万",[unit objectForKey:@"price"]];
NSString *imagearr=[unit objectForKey:@"img"];
NSArray * arr=[imagearr componentsSeparatedByString:@";"];
NSString * path=[NSString stringWithFormat:@"http://121.201.18.34:8080/CAR/upload/car/%@",arr[0]];
UIImage * image=self.imageCache[path];
if (image) {
cell.CarImageView.image=image;
}else {
NSString * cachesPath=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
NSString * filename=[path lastPathComponent];
//计算文件的全路径
NSString * file =[cachesPath stringByAppendingPathComponent:filename];
//加载沙盒的文件数据
NSData * data=[NSData dataWithContentsOfFile:file];
NSLog(@"%@",file);
if (data) {
UIImage * image=[UIImage imageWithData:data];
cell.CarImageView.image=image;
}else{
NSOperation * operation = self.operations[path];
if (operation==nil) {
operation=[NSBlockOperation blockOperationWithBlock:^{
//下载图片
NSData * data=[NSData dataWithContentsOfURL:[NSURL URLWithString:path]];
//数据加载失败
if (data==nil) {
//移除操作
[self.operations removeObjectForKey:path];
return ;
}
UIImage * image=[UIImage imageWithData:data];
//回到主线程显示图片
[[NSOperationQueue mainQueue]addOperationWithBlock:^{
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}];
//存放到字典中
self.imageCache[path]=image;
//将图片写到沙盒中
[data writeToFile:file atomically:YES];
}];
self.operations[path]=operation;
[self.queue addOperation:operation];
}
}
}
// [cell.CarImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://121.201.18.34:8080/CAR/upload/car/%@",arr[0]]]];
}
return cell;
}
转载于:https://www.cnblogs.com/xiezefeng/p/5457924.html
tableview 图片缓存相关推荐
- Android 图片缓存之内存缓存技术LruCache,软引用
Android 图片缓存之内存缓存技术LruCache,软引用
- Android图片缓存之Lru算法
前言: 上篇我们总结了Bitmap的处理,同时对比了各种处理的效率以及对内存占用大小.我们得知一个应用如果使用大量图片就会导致OOM(out of memory),那该如何处理才能近可能的降低oom发 ...
- 图片缓存之内存缓存技术LruCache,软引用
图片缓存之内存缓存技术LruCache,软引用 每当碰到一些大图片的时候,我们如果不对图片进行处理就会报OOM异常, 这个 问题曾经让我觉得很烦恼 ,后来终于得到了解决, 那么现在就让我和大家一起分享 ...
- Android图片缓存框架Glide
Android图片缓存框架Glide Glide是Google提供的一个组件.它具有获取.解码和展示视频剧照.图片.动画等功能.它提供了灵活的API,帮助开发者将Glide应用在几乎任何网络协议栈中. ...
- glide等图片缓存框架替换缓存图片解决方案
2019独角兽企业重金招聘Python工程师标准>>> 应用场景: 手机app 用户的头像地址 在服务器上是固定的,比如有些服务器喜欢以 用户手机或者用户id 作为唯一标识,那么用户 ...
- 【MDCC 2015】开源选型之Android三大图片缓存原理、特性对比
from: http://www.csdn.net/article/2015-10-21/2825984 [CSDN现场报道]10月14日-16日," 2015移动开发者大会 · 中国&qu ...
- 【转载】基于AFNetWorking3.0的图片缓存分析
原文出处:Yasin的简书 http://www.jianshu.com/p/b1045c3fc8d0 图片在APP中占有重要的角色,对图片做好缓存是重要的一项工作. [TOC] 理论 不喜欢理论的可 ...
- android使用ImageLoader实现图片缓存(安卓开发必备)
相信大家在学习以及实际开发中基本都会与网络数据打交道,而这其中一个非常影响用户体验的就是图片的缓存了,若是没有弄好图片缓存,用户体验会大大下降,总会出现卡顿情况,而这个问题尤其容易出现在ListVie ...
- android 缓存文件的工具类,总结的一些android公共库,包含缓存(图片缓存、预取缓存)、...
总结的一些android公共库,包含缓存(图片缓存.预取缓存).公共View(下拉及底部加载更多ListView.底部加载更多ScrollView.滑动一页Gallery).及工具类(下载管理.静默安 ...
最新文章
- Apache Kafka-CMAK(kafka manager)安装部署使用
- is not allowed to connect to this MySql
- Qt|OpenGL学习笔记-绕X轴、Y轴、以及XY轴旋转的四边体
- mysql udf禁用_如何禁用 mysql 的 udf 功能
- java基础之输入语句
- cms完整视频教程+源码 孔浩老师 全131讲
- (一)数据挖掘概念与技术——韩家炜
- OC基础知识(面试必看)
- bootstrap表单样式
- 浏览器 远程桌面连接到服务器,在浏览器中嵌入远程桌面
- linux下mariadb的服务启动异常,centos7 – CentOS 7 MariaDB错误“无法启动mariadb.service:找不到单位.”...
- java gdal开源库_基于GDAL库,读取.grd文件(以海洋地形数据为例)Java版
- 直击14号台风“灿都”最新消息!浙江多地停课停运,上海严阵以待!
- php linux unoconv,Linux-CentOS 安装 Unoconv
- Elastic:运用 Elastic Maps 实时跟踪,可视化资产分布及地理围栏告警(二)
- 第五章 软件项目成本管理
- SQL学习之使用order by 按照指定顺序排序或自定义顺序排序
- 超级小球游戏建立过程
- 论文浅尝 | ERNIE-ViL:从场景图中获取结构化知识来学习视觉语言联合表示
- 如何评估ASO优化方案案例推广效果,优化aso方案