iOS tableView的图片缓存异步加载

1.建立一个viewController.

.h文件实现UIScrollViewDelegate和UITableViewDelegate,并声明ICTableViewDelegate(用来实现图片有缓存则加载图片,无缓存则请求图片并缓存下来再加载)
.h文件如下
#define KimageKey @"photoFileUrl"  ///为数组中每个item中存放图片URL的key名字
#define KidKey @"activityId"    ///为数组中每个item的id  用于缓存之用


#import <UIKit/UIKit.h>
@protocol ICTableViewDelegate
@required
    -(void)cellImageDidLoad:(NSIndexPath *)indexPath image:(NSMutableArray *)imageArray;

@end

@interface ICTableViewController : UIViewController <UIScrollViewDelegate,UITableViewDelegate>
{
@public
    id <ICTableViewDelegate> ICTableVieDelegate;
    NSMutableArray *tableDataArray;
    UITableView *wqTable;
}

 
@end

.m文件如下:

- (void)loadCellImage
{//方法实现实现图片有缓存则加载图片,无缓存则请求图片并缓存下来再加载
    
    NSArray *indexPathsForLoad = [wqTable indexPathsForVisibleRows];
    for (NSIndexPath *item in indexPathsForLoad) {
        NSInteger rowNumberForCell = item.row;
        NSLog(@"%li",(long)rowNumberForCell);
        NSLog(@"%li",(unsigned long)[tableDataArray count]);
        if (rowNumberForCell >[tableDataArray count] -1) {
            return;
        }
        NSString *imageStr =tableDataArray[rowNumberForCell][@"photoFileUrl"];
        NSLog(@"%@",imageStr);
        NSMutableArray *imageArray = [NSMutableArray array];
        if([imageStr length]!=0){
            NSArray *photoUrl = [imageStr componentsSeparatedByString:MULTI_FILES_SEPARATOR];
            for(int i=0;i<photoUrl.count -1;i++){

            
            //显示图片
                NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[NSString stringWithFormat:@"%@/%@%@",[WiseApplicationViewController getImgBucketDomain],[WiseApplicationViewController getOrganizationId],photoUrl[i]] stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]]];
                NSString *imageName = [tableDataArray[rowNumberForCell][KimageKey] stringByAppendingString:[NSString stringWithFormat:@".temp"]];
                NSString *imageDataPath = [NSHomeDirectory() stringByAppendingPathComponent:[@"Library/Caches/" stringByAppendingString:imageName]];
                
                if (![[NSFileManager defaultManager] fileExistsAtPath:imageDataPath]) {
                
                    [imageData writeToFile:imageDataPath atomically:YES];
                    UIImage *image = [UIImage imageWithData:imageData];
                    
                    [imageArray addObject:image];
                    
                    
                }
            
        }
            [ICTableVieDelegate cellImageDidLoad:item image:imageArray];
        }
        
    }
}

#pragma mark - Table View delegate
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{//拖拽之后 完成减速时执行停止滚动时启动缓存加载图片进程
    
    if (!tableView.isDragging && !tableView.isDecelerating)
    {
        [self performSelectorInBackground:@selector(loadCellImage) withObject:nil];
    }
}

#pragma mark - Scroll View delegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{拖拽之后 完成减速时执行启动缓存加载图片进程

    [self performSelectorInBackground:@selector(loadCellImage) withObject:nil];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{//停止滚动时要执行的代码
    if (!decelerate) {
        [self performSelectorInBackground:@selector(loadCellImage) withObject:nil];
    }
}

然后具体子类继承这个父类,并实现ICTableViewDelegate代理方法
#pragma mark ICTableViewDelegate
-(void)cellImageDidLoad:(NSIndexPath *)indexPath image:(NSMutableArray *)imageArray
{
    EventShowTableViewCell *cell = (EventShowTableViewCell *)[_eventListTableView cellForRowAtIndexPath:indexPath];
    if([imageArray count]!=0){
        for(int i=0;i<imageArray.count;i++){
            if (IS_IOS8_OR_LATER) {
                CustomPhotoBtn *photoBtn = (CustomPhotoBtn *)[cell.contentView viewWithTag:(i +1)*10]//<span style="font-family: Arial, Helvetica, sans-serif;">CustomPhotoBtn</span><span style="font-family: Arial, Helvetica, sans-serif;">加载图片封装的一个控件</span>
                UIImage *thumbImg = [FileTransferHelp thumbnailWithImage:(UIImage *)imageArray[i] size:CGSizeMake(60, 40)];
                [photoBtn.imgView setImage:thumbImg];
                [cell.contentView addSubview:photoBtn];

            }else{
                CustomPhotoBtn *photoBtn = (CustomPhotoBtn *)[cell.contentView viewWithTag:(i +1)*10];
                UIImage *thumbImg = [FileTransferHelp thumbnailWithImage:(UIImage *)imageArray[i] size:CGSizeMake(60, 40)];
                [photoBtn.imgView setImage:thumbImg];
                [cell addSubview:photoBtn];

            }
        }
    }
    

}
在子类设置每个cell的内容的

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

里写下
CustomPhotoBtn *photoBtn = [CustomPhotoBtn customPhotoBtn];//加载图片封装的一个控件
    [photoBtn.fileFullName setText:url];
    

    NSString *imageName = [url stringByAppendingString:[NSString stringWithFormat:@".temp"]];
    
    NSLog(@"imageName%@",imageName);
    NSString *imageDataPath = [NSHomeDirectory() stringByAppendingPathComponent:[@"Library/Caches/" stringByAppendingString:imageName]]//从缓存中找图片
    NSLog(@"imageDataPath%@",imageDataPath);
//    [data writeToFile:imageDataPath atomically:YES];
    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfFile:imageDataPath]];
    UIImage *thumbImg = [FileTransferHelp thumbnailWithImage:image size:CGSizeMake(60, 40)];
    if (thumbImg) {
        [photoBtn.imgView setImage:thumbImg];
    }

    
   



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值