网络加载的图片缓存(做本地化保存)

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return  [array count];
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIden = @"myCell";
    static BOOL nibsRegistered = NO;
    
    if (!nibsRegistered) {
        UINib *nib = [UINib nibWithNibName:@"CustomCell" bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:cellIden];
        nibsRegistered = YES;
    }
    
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIden];
    
    if (cell == nil) {
       cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIden];
    }
    
    News *news = [array objectAtIndex:indexPath.row];
    
    cell.NewsTitle.text = news.title;
    
    cell.NewsDetail.text = news.detail;
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    
    NSString *imageLocalFile = [self getImageDocumentsPath:news.imageUrl]; //图片本地的地址
    
    NSLog(@"-------%@",imageLocalFile);
    
    NSFileManager *fileManger = [NSFileManager defaultManager];
    
    //判断图片本地是否存在,若存在则优先加载本地,否则加载发送网络
    if ([fileManger fileExistsAtPath:imageLocalFile]) {
        
        cell.imageView.image = [UIImage imageWithContentsOfFile:imageLocalFile];  //加载本地
    }
    else
    {
        cell.myActivity.hidesWhenStopped = YES;
        [cell.myActivity startAnimating];
        [self loadImage:news.imageUrl withIndexPath:indexPath];    //网络加载图片
        
    }
    
        return cell;

}

- (void)loadImage:(NSString *)imageUrl  withIndexPath:(NSIndexPath *)indexPath
{
    
    
    //异步加载图片
     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
         //加载图片
         NSURL *url = [NSURL URLWithString:imageUrl];
        
         NSData *data = [NSData dataWithContentsOfURL:url];
         
         if (data != nil) {
                     dispatch_async(dispatch_get_main_queue(), ^{
                NSString *imageLocalFile = [self getImageDocumentsPath:imageUrl];
                         if ([data writeToFile:imageLocalFile atomically:YES]==NO) {   // 图片本地化保存
                             NSLog(@"图片本地化保存失败!");
                         }
                 
                 
                 CustomCell *cell = (CustomCell *)[self.myTableView cellForRowAtIndexPath:indexPath];
                

                 cell.imageView.image = [UIImage imageWithData:data];
                 
                 [cell.myActivity  stopAnimating];
                 
             });
             
         }
         
     });
    
    
}

- (NSString *)getImageDocumentsPath:(NSString *)imageUrl
{
    NSLog(@"%@",imageUrl);   //imageUrl形如:http://img1.cache.netease.com/3g/2014/8/11/2014081100332746e6f.jpg

    NSArray *array_ = [imageUrl componentsSeparatedByString:@"/"];   //网络imageUrl通过分隔符分隔开来
    
    NSString *string = [NSString stringWithFormat:@"%@/Documents/%@",NSHomeDirectory(),[array_ lastObject]];       / /组装本地化存储一个存储网络图片的地址,形如:/Users/administrator/Library/Application Support/iPhone Simulator/7.0.3/Applications/16071117-BE38-4F37-8FEF-4AF438C053A0/Documents/2014090111135873241.jpg
    
    return string;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值