需要对图片进行缓存 写入沙盒中,使用多线程下载图将队列放到字典中,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 图片缓存相关推荐

  1. Android 图片缓存之内存缓存技术LruCache,软引用

    Android 图片缓存之内存缓存技术LruCache,软引用

  2. Android图片缓存之Lru算法

    前言: 上篇我们总结了Bitmap的处理,同时对比了各种处理的效率以及对内存占用大小.我们得知一个应用如果使用大量图片就会导致OOM(out of memory),那该如何处理才能近可能的降低oom发 ...

  3. 图片缓存之内存缓存技术LruCache,软引用

    图片缓存之内存缓存技术LruCache,软引用 每当碰到一些大图片的时候,我们如果不对图片进行处理就会报OOM异常, 这个 问题曾经让我觉得很烦恼 ,后来终于得到了解决, 那么现在就让我和大家一起分享 ...

  4. Android图片缓存框架Glide

    Android图片缓存框架Glide Glide是Google提供的一个组件.它具有获取.解码和展示视频剧照.图片.动画等功能.它提供了灵活的API,帮助开发者将Glide应用在几乎任何网络协议栈中. ...

  5. glide等图片缓存框架替换缓存图片解决方案

    2019独角兽企业重金招聘Python工程师标准>>> 应用场景: 手机app 用户的头像地址 在服务器上是固定的,比如有些服务器喜欢以 用户手机或者用户id 作为唯一标识,那么用户 ...

  6. 【MDCC 2015】开源选型之Android三大图片缓存原理、特性对比

    from: http://www.csdn.net/article/2015-10-21/2825984 [CSDN现场报道]10月14日-16日," 2015移动开发者大会 · 中国&qu ...

  7. 【转载】基于AFNetWorking3.0的图片缓存分析

    原文出处:Yasin的简书 http://www.jianshu.com/p/b1045c3fc8d0 图片在APP中占有重要的角色,对图片做好缓存是重要的一项工作. [TOC] 理论 不喜欢理论的可 ...

  8. android使用ImageLoader实现图片缓存(安卓开发必备)

    相信大家在学习以及实际开发中基本都会与网络数据打交道,而这其中一个非常影响用户体验的就是图片的缓存了,若是没有弄好图片缓存,用户体验会大大下降,总会出现卡顿情况,而这个问题尤其容易出现在ListVie ...

  9. android 缓存文件的工具类,总结的一些android公共库,包含缓存(图片缓存、预取缓存)、...

    总结的一些android公共库,包含缓存(图片缓存.预取缓存).公共View(下拉及底部加载更多ListView.底部加载更多ScrollView.滑动一页Gallery).及工具类(下载管理.静默安 ...

最新文章

  1. Apache Kafka-CMAK(kafka manager)安装部署使用
  2. is not allowed to connect to this MySql
  3. Qt|OpenGL学习笔记-绕X轴、Y轴、以及XY轴旋转的四边体
  4. mysql udf禁用_如何禁用 mysql 的 udf 功能
  5. java基础之输入语句
  6. cms完整视频教程+源码 孔浩老师 全131讲
  7. (一)数据挖掘概念与技术——韩家炜
  8. OC基础知识(面试必看)
  9. bootstrap表单样式
  10. 浏览器 远程桌面连接到服务器,在浏览器中嵌入远程桌面
  11. linux下mariadb的服务启动异常,centos7 – CentOS 7 MariaDB错误“无法启动mariadb.service:找不到单位.”...
  12. java gdal开源库_基于GDAL库,读取.grd文件(以海洋地形数据为例)Java版
  13. 直击14号台风“灿都”最新消息!浙江多地停课停运,上海严阵以待!
  14. php linux unoconv,Linux-CentOS 安装 Unoconv
  15. Elastic:运用 Elastic Maps 实时跟踪,可视化资产分布及地理围栏告警(二)
  16. 第五章 软件项目成本管理
  17. SQL学习之使用order by 按照指定顺序排序或自定义顺序排序
  18. 超级小球游戏建立过程
  19. 论文浅尝 | ERNIE-ViL:从场景图中获取结构化知识来学习视觉语言联合表示
  20. 如何评估ASO优化方案案例推广效果,优化aso方案

热门文章

  1. python去重计数_Python list去重及找出,统计重复项
  2. java聚合_Java™ 教程(聚合归纳操作)
  3. spring和spring_Spring MVCGradle
  4. java金字塔程序_Java中的金字塔模式程序
  5. 在Selenium Chrome驱动程序上运行测试
  6. 若想活得洒脱,就要学会看开
  7. Java面试题:程序计数器为什么是私有的?
  8. C++的冒泡排序法的原理是什么?
  9. RT-Thread源码学习第六篇,线程调度器(1)
  10. nginx虚拟域名配置