你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS tableView的圖片緩存異步加載

iOS tableView的圖片緩存異步加載

編輯:IOS開發綜合

1.建立一個viewController.

.h文件實現UIScrollViewDelegate和UITableViewDelegate,並聲明ICTableViewDelegate(用來實現圖片有緩存則加載圖片,無緩存則請求圖片並緩存下來再加載) .h文件如下
#define KimageKey @"photoFileUrl"  ///為數組中每個item中存放圖片URL的key名字
#define KidKey @"activityId"    ///為數組中每個item的id  用於緩存之用


#import 
@protocol ICTableViewDelegate
@required
    -(void)cellImageDidLoad:(NSIndexPath *)indexPath image:(NSMutableArray *)imageArray;

@end

@interface ICTableViewController : UIViewController 
{
@public
    id  WQTableVieDelegate;
    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

然後具體子類繼承這個父類,並實現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;iCustomPhotoBtn加載圖片封裝的一個控件
                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];
    }

    
    return photoBtn;



  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved