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中显示出来,但每次都要从网络上请求,会严重影响用 ...
随机推荐
- openwrt上网配置的一些理解
其实已经有很多帖子讲过openwrt路由器上网配置了,我这里主要是讲我自己的一块硬件路由使用openwrt后的一些上网配置.之所以要研究我自己的配置,是因为硬件,硬件不一样,配置也就不一样,但是总的原 ...
- C#委托的详细使用
代码如下: public delegate void GreetingDelegate(string name);//定义委托,它定义了可以代表方法的类型 class Program { public ...
- (转)[老老实实学WCF] 第二篇 配置WCF
在上一篇中,我们在一个控制台应用程序中编写了一个简单的WCF服务并承载了它.先回顾一下服务端的代码: using System; using System.Collections.Generic; u ...
- jedis处理redis cluster集群的密码问题
环境介绍:jedis:2.8.0 redis版本:3.2 首先说一下redis集群的方式,一种是cluster的 一种是sentinel的,cluster的是redis 3.0之后出来新的集群方式 本 ...
- Silverlight visifire Chart图表下载到PPT文件中
一.Silverlight xaml.cs文件 1. //下载图表 private void btnDown_Click(object sender, RoutedEventArgs e ...
- iOS import导入pod第三方库不提示问题
pod 导入第三方库后,使用import 不提示第三方库头文件. 解决办法: 选择target -> BuildSettings -> search Paths 下的 User Heade ...
- js中的因数分解
方法一: <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8& ...
- Shiro 权限框架使用总结
我们首先了解下什么是shiro ,Shiro 是 JAVA 世界中新近出现的权限框架,较之 JAAS 和 Spring Security,Shiro 在保持强大功能的同时,还在简单性和灵活性方面拥有巨 ...
- 清空文本框SetDlgItemText(IDC_TXTXT,NULL);
清空文本框 SetDlgItemText(IDC_TXTXT,NULL);
- 多线程中遇到ASSERT(pMap->LookupPermanent(hWndOrig) == NULL);怎么解决
XP下用VC开发的程序,在一个主线程调用3 个线程,线程之间要共享数据,结果总出现wincore.cpp line 980 ASSERT(pMap-> LookupPermane ...