UITableView缓存加载图片

来源:互联网 发布:手机改ip软件 编辑:程序博客网 时间:2024/06/05 13:29

- (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     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];    if (cell == nil) {        cell         cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]autorelease];    }    NSDictionary     }    NSDictionary *item = [self.listTableViewArray objectAtIndex:indexPath.row];        cell.textLabel.text     cell.textLabel.text =[[item objectForKey:@"im:name"]objectForKey:@"label"];    NSString     NSString *imageName = [[[[self.listTableViewArray objectAtIndex:indexPath.row] valueForKey:@"im:name"] valueForKey:@"label"] stringByAppendingString:@".temp"];        //先到Library/Caches/ 目录下找图片 如果没有 就使用默认图片    NSString     //先到Library/Caches/ 目录下找图片 如果没有 就使用默认图片    NSString *imageDataPath = [NSHomeDirectory() stringByAppendingPathComponent:[@"Library/Caches/" stringByAppendingString:imageName]];    UIImage     UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfFile:imageDataPath]];    if (image) {        cell.imageView.image         cell.imageView.image = image;    }        }else {        cell.imageView.image         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
        }
        
        
        
        
    }
}

-(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        }
        
        
        
        
    }
}

-(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
        }

        
        
        
        
    }
}
}
-(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
        }
        
        
        
        
    }
}
摘自http://www.cnblogs.com/gaoxiao228/archive/2012/05/28/2522683.html
0 0
原创粉丝点击