码迷,mamicode.com
首页 > 其他好文 > 详细

tableview 图片缓存

时间:2016-05-04 13:12:10      阅读:102      评论:0      收藏:0      [点我收藏+]

标签:

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

tableview 图片缓存

标签:

原文地址:http://www.cnblogs.com/xiezefeng/p/5457924.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!