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];
}

iOS tableView的图片缓存异步载入的更多相关文章

  1. Fast Image Cache – iOS 应用程序高性能图片缓存

    Fast Image Cache 是一种在 iOS 应用程序中高效.持续.超快速的存储和检索图像的解决方案.任何良好的 iOS 应用程序的用户体验都应该是快速,平滑滚动的,Fast Image Cac ...

  2. ListView的异步载入(笔记,多线程和AsyncTask)

    异步载入最经常使用的两种方式: 多线程,线程池     AsyncTask 实例操作: 从一个站点上获取Json数据.然后将数据在ListView上显示. 1.创建item_layout布局 , 改动 ...

  3. 页面优化——js异步载入

    同步载入 在介绍js异步载入之前.我们先来看看什么是js同步载入.我们平时最常使用的就是这样的同步载入形式: <script src="http://XXX.com/script.js ...

  4. Android批量图片载入经典系列——afinal框架实现图片的异步缓存载入

    一.问题描写叙述 在之前的系列文章中,我们使用了Volley和Xutil框架实现图片的缓存载入,接下来我们再介绍一下afinal 框架的使用. Afinal 是一个android的http框架.sql ...

  5. (源代码分析)Android-Universal-Image-Loader (图片异步载入缓存库)的使用配置

    转载请注明出处:http://blog.csdn.net/u011733020 前言: 在Android开发中,对于图片的载入能够说是个老生常谈的问题了,图片载入是一个比較坑的地方.处理不好,会有各种 ...

  6. tableView异步下载图片/SDWebImage图片缓存原理

    问题说明:假设tableView的每个cell上的imageView的image都是从网络上获取的数据.如何解决图片延迟加载(显示很慢).程序卡顿.图片错误显示.图片跳动的问题. 需要解决的问题: 1 ...

  7. iOS图片缓存框架SDWebImage

    本文转发至: http://blog.csdn.net/uxyheaven/article/details/7909373 http://www.cocoachina.com/ios/20141212 ...

  8. iOS UIImage DownLoad图片的下载缓存全部在此

    iOS图片的下载缓存全部在此 分类: iOS编程 -- : 2075人阅读 评论() 收藏 举报 注意: 我的文章只写给自己看 ------------------------------------ ...

  9. iOS网络加载图片缓存策略之ASIDownloadCache缓存优化

    iOS网络加载图片缓存策略之ASIDownloadCache缓存优化   在我们实际工程中,很多情况需要从网络上加载图片,然后将图片在imageview中显示出来,但每次都要从网络上请求,会严重影响用 ...

随机推荐

  1. poj 1011 Sticks (DFS+剪枝)

    Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 127771   Accepted: 29926 Descrip ...

  2. [Redux] Extracting Presentational Components -- Todo, TodoList

    Code to be refactored: let nextTodoId = 0; class TodoApp extends Component { render() { const { todo ...

  3. Git diff (---和+++具体解释)

    如果两个文件相似度很高,那么上下文格式的diff,将显示大量重复的内容,很浪费空间.1990年,GNU diff率先推出了"合并格式"的diff,将f1和f2的上下文合并在一起显示 ...

  4. 将excel的.xlsx文件转成数据库文件.db的方法

    1.下载好工具SQLite Expert Professional 3 ,晚上有很多,自行百度. 2.打开.xls文件,将文件另存为.csv文件.说明一下: 可以将xls文件看作是数据库里的一个表.所 ...

  5. Oracle - SQL 错误: ORA-00917: 缺失逗号

    ORACLE SQL语句中int型插入数据库时,不要加引号.

  6. IL(Intermediate Language)

    释义: IL是.NET框架中中间语言(Intermediate Language)的缩写.使用.NET框架提供的编译器可以直接将源程序编译为.exe或.dll文件,但此时编译出来的程序代码并不是CPU ...

  7. ios--图片处理(修改、保存)UIGraphicsBeginImageContext

    http://www.th7.cn/Program/IOS/201407/250904.shtml

  8. ESP8266固件修改可以控制多个IO方法

    之前在论坛上找到了一个通过ESP8266可以控制GPIO0的固件和app,但是自己做的家庭影音灯光系统是需要控制多个IO从而控制STM32.通过观看大明的视频,了解了GPIO的控制方法. 在固件的ap ...

  9. [转]C++学习心得

    1.把C++当成一门新的语言学习: 2.看<Thinking In C++>: 3.看<The C++ Programming Language>和<Inside The ...

  10. CentOS和Ubuntu的区别

    CentOS(Community ENTerprise Operating System)是Linux发行版之一,它是来自于Red Hat Enterprise Linux依照开放源代码规定释出的源代 ...