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. laravel3中文文档是迈入laravel4的捷径

    http://v3.golaravel.com/docs/ 目录 Laravel概览 更新日志 安装与设置 系统需求 安装 服务器设置 基本设置 环境 友好的链接(URL) 路由 基础 通配符(Wil ...

  2. [RxJS] Changing Behavior with MapTo

    You often need streams to trigger different behaviors on the data based on which streams triggers. T ...

  3. std::remove_if

    原型: #include <algorithm>forward_iterator remove_if( forward_iterator start, forward_iterator e ...

  4. C语言随记-1

    涉及指针.数组.函数指针 几种声明形式 int *a[5]; // a是一个有5个元素的数组,每个元素是整数类型指针(int *) int *a[] = {0x100, 0x104, 0x108, 0 ...

  5. XML基本知识

    一.xml简介 1.xml(可扩展标记语言),是一种标记语言,类似于html,其作用主要是传输数据,并非显示数据! 2.xml标签没有被预定义需要用户自行定义. 3.xml由w3c组织发布,遵循200 ...

  6. EF中的TPH、TPT、TPC

    1. Table Per Hierarchy(TPH):只建立一个表,把基类和子类中的所有属性都映射为表中的列2. Table Per Type(TPT):为基类和每个子类建立一个表,每个与子类对应的 ...

  7. jquery悬停tab

    <style> *{ margin:0; padding:0;} body { font:12px/19px Arial, Helvetica, sans-serif; color:#66 ...

  8. AngularJs练习Demo2

    @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport&quo ...

  9. zeromq源码分析笔记之无锁队列ypipe_t(3)

    在上一篇中说到了mailbox_t的底层实际上使用了管道ypipe_t来存储命令.而ypipe_t实质上是一个无锁队列,其底层使用了yqueue_t队列,ypipe_t是对yueue_t的再包装,所以 ...

  10. 对面向对象程序设计(OOP)的认识

    前言 本文主要介绍面向对象(OO)程序设计,以维基百科的解释: 面向对象程序设计(英语:Object-oriented programming,缩写:OOP),指一种程序设计范型,同时也是一种程序开发 ...