UITableView 异步加载图片缓存 笔记



在cocoachina 上看到一篇文章 http://www.cocoachina.com/newbie/basic/2012/0511/4237.html UITableView 异步加载图片缓存
正好我也在学习UITableView 所以模仿了一个 拿来练手
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
    self.listTableViewArray = [NSMutableArray arrayWithCapacity:1];
    
    
    NSString *jsonStr = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://phobos.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/toppaidapplications/limit=75/json"] encoding:NSUTF8StringEncoding error:nil];
    NSDictionary *dic = [jsonStr JSONValue];//通过json 从网络上获得数据
    
    self.listTableViewArray = [[dic objectForKey:@"feed"]objectForKey:@"entry"];
    
    
    table = [[[UITableView alloc]initWithFrame:CGRectMake(100, 100, 320, 480) ]autorelease];
    [self.view addSubview:table];
    table.backgroundColor = [UIColor yellowColor];
    table.delegate = self; // 设置代理
    table.dataSource = self;// 设置数据源
    // Do any additional setup after loading the view, typically from a nib.
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [self.listTableViewArray count];//返回cell的函数 } -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *identifier = @"ddddd"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; if (cell == nil) { cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]autorelease]; } NSDictionary *item = [self.listTableViewArray objectAtIndex:indexPath.row]; cell.textLabel.text =[[item objectForKey:@"im:name"]objectForKey:@"label"]; NSString *imageName = [[[[self.listTableViewArray objectAtIndex:indexPath.row] valueForKey:@"im:name"] valueForKey:@"label"] stringByAppendingString:@".temp"]; //先到Library/Caches/ 目录下找图片 如果没有 就使用默认图片 NSString *imageDataPath = [NSHomeDirectory() stringByAppendingPathComponent:[@"Library/Caches/" stringByAppendingString:imageName]]; UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfFile:imageDataPath]]; if (image) { cell.imageView.image = image; } else { cell.imageView.image = [UIImage imageNamed:@"Placeholder.png"]; //placeholder为在未加载完成加载图片时显示的默认图片 } return cell; }

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
// 因为UITableView 继承的是UIScrollView 所以继承方法scrollViewDidEndDecelerating 这个方法是 拖拽 scroll之后 完成减速时执行
    [self performSelectorInBackground:@selector(loadCellImage) withObject:nil]; //另开线程 执行加载cell里的图片
    NSLog(@"scrollViewDidEndDecelerating");
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{//行将显示的时候调用
    if (!tableView.isDragging && !tableView.isDecelerating)
    {
        [self performSelectorInBackground:@selector(loadCellImage) withObject:nil];
        NSLog(@" table view  willDisplayCell");
    }
   
}
-(void)loadCellImage
{
    NSArray *indexPathsForLoad = [table indexPathsForVisibleRows];// 获得 table 当前显示的cells
    for (NSIndexPath *item in indexPathsForLoad) {
        NSInteger rowNumberForCell = item.row;
        NSString *imageStr = [[[[self.listTableViewArray objectAtIndex:rowNumberForCell]objectForKey:@"im:image"]objectAtIndex:0]objectForKey:@"label"];
        NSString *imageName = [[[[self.listTableViewArray objectAtIndex:rowNumberForCell]valueForKey:@"im:name"]valueForKey:@"label"]stringByAppendingString:@".temp"];
        NSString *imageDataPath = [NSHomeDirectory() stringByAppendingPathComponent:[@"Library/Caches/" stringByAppendingString:imageName]];
        if (![[NSFileManager defaultManager]fileExistsAtPath:imageDataPath]) {//先判断Library/Caches/ 这个目录下有没有这个文件如果没有 就写入目录
            NSURL *imageurl = [NSURL URLWithString:[imageStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
            NSData *imageData = [NSData dataWithContentsOfURL:imageurl];
            [imageData writeToFile:imageDataPath atomically:YES];
            UIImage *image = [UIImage imageWithData:imageData];
            UITableViewCell *cell = [table cellForRowAtIndexPath:item];
            cell.imageView.image = image;// 更新cell的 image
        }
        
        
        
        
    }
}

转载于:https://www.cnblogs.com/gaoxiao228/archive/2012/05/28/2522683.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值