iOS tableView的图片缓存异步载入
1.建立一个viewController.
#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的图片缓存异步载入的更多相关文章
- Fast Image Cache – iOS 应用程序高性能图片缓存
Fast Image Cache 是一种在 iOS 应用程序中高效.持续.超快速的存储和检索图像的解决方案.任何良好的 iOS 应用程序的用户体验都应该是快速,平滑滚动的,Fast Image Cac ...
- ListView的异步载入(笔记,多线程和AsyncTask)
异步载入最经常使用的两种方式: 多线程,线程池 AsyncTask 实例操作: 从一个站点上获取Json数据.然后将数据在ListView上显示. 1.创建item_layout布局 , 改动 ...
- 页面优化——js异步载入
同步载入 在介绍js异步载入之前.我们先来看看什么是js同步载入.我们平时最常使用的就是这样的同步载入形式: <script src="http://XXX.com/script.js ...
- Android批量图片载入经典系列——afinal框架实现图片的异步缓存载入
一.问题描写叙述 在之前的系列文章中,我们使用了Volley和Xutil框架实现图片的缓存载入,接下来我们再介绍一下afinal 框架的使用. Afinal 是一个android的http框架.sql ...
- (源代码分析)Android-Universal-Image-Loader (图片异步载入缓存库)的使用配置
转载请注明出处:http://blog.csdn.net/u011733020 前言: 在Android开发中,对于图片的载入能够说是个老生常谈的问题了,图片载入是一个比較坑的地方.处理不好,会有各种 ...
- tableView异步下载图片/SDWebImage图片缓存原理
问题说明:假设tableView的每个cell上的imageView的image都是从网络上获取的数据.如何解决图片延迟加载(显示很慢).程序卡顿.图片错误显示.图片跳动的问题. 需要解决的问题: 1 ...
- iOS图片缓存框架SDWebImage
本文转发至: http://blog.csdn.net/uxyheaven/article/details/7909373 http://www.cocoachina.com/ios/20141212 ...
- iOS UIImage DownLoad图片的下载缓存全部在此
iOS图片的下载缓存全部在此 分类: iOS编程 -- : 2075人阅读 评论() 收藏 举报 注意: 我的文章只写给自己看 ------------------------------------ ...
- iOS网络加载图片缓存策略之ASIDownloadCache缓存优化
iOS网络加载图片缓存策略之ASIDownloadCache缓存优化 在我们实际工程中,很多情况需要从网络上加载图片,然后将图片在imageview中显示出来,但每次都要从网络上请求,会严重影响用 ...
随机推荐
- 《第一行代码》学习笔记21-Git
Git(1) 1.Git是一个开源的分布式版本控制工具,其开发者是Linux操作系统的作者Linus Torvalds. 2.仓库(Repository)是用于保存版本管理所需要信息的地方,所有本地提 ...
- (一)backbone - API入门
初探 backbone采用MVC模式,本身提供了模型.控制器和视图从而我们应用程序的骨架便形成. Backbone.js 唯一重度依赖 Underscore.js. 对于 RESTful , hist ...
- 数据画图 jpgraph & chart.js
今天想到要研究下“用图表的形式来呈现数据”这个主题.对比了下两种实现的方法: 方法一:通过php代码在服务器端生成图像,再将图像传回客户端.使用jpGraph类库. 方法二:通过js和html5技术, ...
- python代码风格规范
类注释模板: :: class AnotherClass: """ 类注释 """ def method(self, arg1, arg2, ...
- Taum and B'day
//自己 def main(): t = int(raw_input()) for _ in range(t): units = 0 b, w = map(int, raw_input().strip ...
- CSU 1335 高桥和低桥
开始队友说是线段树,看了看貌似也是,上手敲了个嵌套的线段树,O(nlognlogn)的复杂度果断tle了 TAT 思路:对h[i]排序,对每次涨水退水,先用二分查找,再用一个数组保存当前点之后所有点被 ...
- AFNetworking实现 断点续传
用AFNetworking实现断点续传,暂停,继续 AFNetworking断点续传暂停恢复 AFNetworking的版本:platform:ios,'7.0' pod "AFNetw ...
- iOS中构造函数与析构函数
一.构造函数 在OC中凡是已init开头的函数我们都称之为构造函数,在声明构造函数的时候,不带参数的一般直接声明为“-(id)init”,带参数的一般声明为“-(id)initWith...”. 1 ...
- android获取sdk更新
http://www.th7.cn/Program/Android/201310/154981.shtml 网上许多解决 Android SDK Manager 无法更新(下载)的问题的方法基本都是将 ...
- delphi 打开文件夹并定位到一个文件(关键是/select参数)
strFileName := FcxLV[nIndex].Items.Item[FcxLV[nIndex].ItemIndex].SubItems.Strings[0]; //路径 ShellExe ...