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中显示出来,但每次都要从网络上请求,会严重影响用 ...
随机推荐
- altium designer 原理图复制出错
复制原理图的时候最后弹出这种错误 InvalidParameter at 2510219C. AdvSch.dll, Base Address: 24C80000. Exception Occurre ...
- JMeter基础概念
JMeter 介绍:一个非常优秀的开源的性能测试工具. 优点:你用着用着就会发现它的重多优点,当然不足点也会呈现出来. 从性能工具的原理划分: Jmeter工具和其他性能工具在原理上完全一致,工具包含 ...
- [Cycle.js] Generalizing run() function for more types of sources
Our application was able to produce write effects, through sinks, and was able to receive read effec ...
- Android 实时文件夹
实时文件夹是一种用来显示由某个ContentProvider提供的数据信息的桌面组件.要创建一个实时文件夹,必须要有两个方面的支持. 1,要定义一个用来创建实时文件夹的Activity. 2,所指定数 ...
- 用CSS3写的钟表
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- jquery悬停tab2
<style> *{ margin:0; padding:0;} body { font:12px/19px Arial, Helvetica, sans-serif; color:#66 ...
- 浅谈Block传值-匿名函数(代码块)
block传值是从后往前传值---代理也是 // 使用block时, 不能访问self, 也不能直接访问属性, self.属性, 用self调用方法; 只要这样做了, block都会对其强引用一份, ...
- HTML解析利器 - HtmlAgilityPack
HtmlAgilityPack 是CodePlex 上的一个开源项目.它提供了标准的DOM API 和XPath 导航--即使 HTML 不是适当的格式! 使用HtmlAgilityPack操作HTM ...
- GDB调试一
http://blog.csdn.net/haoel/article/details/2881 GDB主要调试的是C/C++的程序.要调试C/C++的程序,首先在编译时,我们必须要把调试信息加到可执行 ...
- .Net 插入数据MySql数据库,中文乱码解决问题
1, 修改mysql根目录下配置文件my.ini,在[client]节点下添加default-character-set=utf8 ,在[mysqld]节点下添加character_set_serve ...