UITableView缓存加载图片
来源:互联网 发布:手机改ip软件 编辑:程序博客网 时间:2024/06/05 13:29
- (void)viewDidLoad{ [super viewDidLoad]; self.listTableViewArray = [NSMutableArray arrayWithCapacity:1]; NSString *jsonStr = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://phobos.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/toppaidapplications/limit=75/json"] encoding:NSUTF8StringEncoding error:nil]; NSDictionary *dic = [jsonStr JSONValue];//通过json 从网络上获得数据 self.listTableViewArray = [[dic objectForKey:@"feed"]objectForKey:@"entry"]; table = [[[UITableView alloc]initWithFrame:CGRectMake(100, 100, 320, 480) ]autorelease]; [self.view addSubview:table]; table.backgroundColor = [UIColor yellowColor]; table.delegate = self; // 设置代理 table.dataSource = self;// 设置数据源 // Do any additional setup after loading the view, typically from a nib.}-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ {return 1;}}-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ {return [self.listTableViewArray count];//返回cell的函数 }}-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ {static NSString *identifier = @"ddddd"; UITableViewCell UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; if (cell == nil) { cell cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]autorelease]; } NSDictionary } NSDictionary *item = [self.listTableViewArray objectAtIndex:indexPath.row]; cell.textLabel.text cell.textLabel.text =[[item objectForKey:@"im:name"]objectForKey:@"label"]; NSString NSString *imageName = [[[[self.listTableViewArray objectAtIndex:indexPath.row] valueForKey:@"im:name"] valueForKey:@"label"] stringByAppendingString:@".temp"]; //先到Library/Caches/ 目录下找图片 如果没有 就使用默认图片 NSString //先到Library/Caches/ 目录下找图片 如果没有 就使用默认图片 NSString *imageDataPath = [NSHomeDirectory() stringByAppendingPathComponent:[@"Library/Caches/" stringByAppendingString:imageName]]; UIImage UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfFile:imageDataPath]]; if (image) { cell.imageView.image cell.imageView.image = image; } }else { cell.imageView.image cell.imageView.image = [UIImage imageNamed:@"Placeholder.png"]; //placeholder为在未加载完成加载图片时显示的默认图片 }return cell;}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
// 因为UITableView 继承的是UIScrollView 所以继承方法scrollViewDidEndDecelerating 这个方法是 拖拽 scroll之后 完成减速时执行
[self performSelectorInBackground:@selector(loadCellImage) withObject:nil]; //另开线程 执行加载cell里的图片
NSLog(@"scrollViewDidEndDecelerating");
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{//行将显示的时候调用
if (!tableView.isDragging && !tableView.isDecelerating)
{
[self performSelectorInBackground:@selector(loadCellImage) withObject:nil];
NSLog(@" table view willDisplayCell");
}
}
-(void)loadCellImage
{
NSArray *indexPathsForLoad = [table indexPathsForVisibleRows];// 获得 table 当前显示的cells
for (NSIndexPath *item in indexPathsForLoad) {
NSInteger rowNumberForCell = item.row;
NSString *imageStr = [[[[self.listTableViewArray objectAtIndex:rowNumberForCell]objectForKey:@"im:image"]objectAtIndex:0]objectForKey:@"label"];
NSString *imageName = [[[[self.listTableViewArray objectAtIndex:rowNumberForCell]valueForKey:@"im:name"]valueForKey:@"label"]stringByAppendingString:@".temp"];
NSString *imageDataPath = [NSHomeDirectory() stringByAppendingPathComponent:[@"Library/Caches/" stringByAppendingString:imageName]];
if (![[NSFileManager defaultManager]fileExistsAtPath:imageDataPath]) {//先判断Library/Caches/ 这个目录下有没有这个文件如果没有 就写入目录
NSURL *imageurl = [NSURL URLWithString:[imageStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData *imageData = [NSData dataWithContentsOfURL:imageurl];
[imageData writeToFile:imageDataPath atomically:YES];
UIImage *image = [UIImage imageWithData:imageData];
UITableViewCell *cell = [table cellForRowAtIndexPath:item];
cell.imageView.image = image;// 更新cell的 image
}
}
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{// 因为UITableView 继承的是UIScrollView 所以继承方法scrollViewDidEndDecelerating 这个方法是 拖拽 scroll之后 完成减速时执行 [self performSelectorInBackground:@selector(loadCellImage) withObject:nil]; //另开线程 执行加载cell里的图片 NSLog(@"scrollViewDidEndDecelerating");}-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{//行将显示的时候调用 if (!tableView.isDragging && !tableView.isDecelerating) { [self performSelectorInBackground:@selector(loadCellImage) withObject:nil]; NSLog(@" table view willDisplayCell"); } }-(void)loadCellImage{ NSArray *indexPathsForLoad = [table indexPathsForVisibleRows];// 获得 table 当前显示的cells for (NSIndexPath *item in indexPathsForLoad) { NSInteger rowNumberForCell = item.row; NSString *imageStr = [[[[self.listTableViewArray objectAtIndex:rowNumberForCell]objectForKey:@"im:image"]objectAtIndex:0]objectForKey:@"label"]; NSString *imageName = [[[[self.listTableViewArray objectAtIndex:rowNumberForCell]valueForKey:@"im:name"]valueForKey:@"label"]stringByAppendingString:@".temp"]; NSString *imageDataPath = [NSHomeDirectory() stringByAppendingPathComponent:[@"Library/Caches/" stringByAppendingString:imageName]]; if (![[NSFileManager defaultManager]fileExistsAtPath:imageDataPath]) {//先判断Library/Caches/ 这个目录下有没有这个文件如果没有 就写入目录 NSURL *imageurl = [NSURL URLWithString:[imageStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; NSData *imageData = [NSData dataWithContentsOfURL:imageurl]; [imageData writeToFile:imageDataPath atomically:YES]; UIImage *image = [UIImage imageWithData:imageData]; UITableViewCell *cell = [table cellForRowAtIndexPath:item]; cell.imageView.image = image;// 更新cell的 image }
}
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
// 因为UITableView 继承的是UIScrollView 所以继承方法scrollViewDidEndDecelerating 这个方法是 拖拽scroll之后 完成减速时执行
[self performSelectorInBackground:@selector(loadCellImage) withObject:nil]; //另开线程 执行加载cell里的图片
NSLog(@"scrollViewDidEndDecelerating");
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{//行将显示的时候调用
if (!tableView.isDragging && !tableView.isDecelerating)
{
[self performSelectorInBackground:@selector(loadCellImage) withObject:nil];
NSLog(@" table view willDisplayCell");
}
}
-(void)loadCellImage
{
NSArray *indexPathsForLoad = [table indexPathsForVisibleRows];// 获得 table 当前显示的cells
for (NSIndexPath *item in indexPathsForLoad) {
NSInteger rowNumberForCell = item.row;
NSString *imageStr = [[[[self.listTableViewArray objectAtIndex:rowNumberForCell]objectForKey:@"im:image"]objectAtIndex:0]objectForKey:@"label"];
NSString *imageName = [[[[self.listTableViewArray objectAtIndex:rowNumberForCell]valueForKey:@"im:name"]valueForKey:@"label"]stringByAppendingString:@".temp"];
NSString *imageDataPath = [NSHomeDirectory() stringByAppendingPathComponent:[@"Library/Caches/" stringByAppendingString:imageName]];
if (![[NSFileManager defaultManager]fileExistsAtPath:imageDataPath]) {//先判断Library/Caches/ 这个目录下有没有这个文件如果没有 就写入目录
NSURL *imageurl = [NSURL URLWithString:[imageStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData *imageData = [NSData dataWithContentsOfURL:imageurl];
[imageData writeToFile:imageDataPath atomically:YES];
UIImage *image = [UIImage imageWithData:imageData];
UITableViewCell *cell = [table cellForRowAtIndexPath:item];
cell.imageView.image = image;// 更新cell的 image
}
}
}
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
// 因为UITableView 继承的是UIScrollView 所以继承方法scrollViewDidEndDecelerating 这个方法是 拖拽scroll之后 完成减速时执行
[self performSelectorInBackground:@selector(loadCellImage) withObject:nil]; //另开线程 执行加载cell里的图片
NSLog(@"scrollViewDidEndDecelerating");
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{//行将显示的时候调用
if (!tableView.isDragging && !tableView.isDecelerating)
{
[self performSelectorInBackground:@selector(loadCellImage) withObject:nil];
NSLog(@" table view willDisplayCell");
}
}
-(void)loadCellImage
{
NSArray *indexPathsForLoad = [table indexPathsForVisibleRows];// 获得 table 当前显示的cells
for (NSIndexPath *item in indexPathsForLoad) {
NSInteger rowNumberForCell = item.row;
NSString *imageStr = [[[[self.listTableViewArray objectAtIndex:rowNumberForCell]objectForKey:@"im:image"]objectAtIndex:0]objectForKey:@"label"];
NSString *imageName = [[[[self.listTableViewArray objectAtIndex:rowNumberForCell]valueForKey:@"im:name"]valueForKey:@"label"]stringByAppendingString:@".temp"];
NSString *imageDataPath = [NSHomeDirectory() stringByAppendingPathComponent:[@"Library/Caches/" stringByAppendingString:imageName]];
if (![[NSFileManager defaultManager]fileExistsAtPath:imageDataPath]) {//先判断Library/Caches/ 这个目录下有没有这个文件如果没有 就写入目录
NSURL *imageurl = [NSURL URLWithString:[imageStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData *imageData = [NSData dataWithContentsOfURL:imageurl];
[imageData writeToFile:imageDataPath atomically:YES];
UIImage *image = [UIImage imageWithData:imageData];
UITableViewCell *cell = [table cellForRowAtIndexPath:item];
cell.imageView.image = image;// 更新cell的 image
}
}
}
- (void)viewDidLoad{ [super viewDidLoad]; self.listTableViewArray = [NSMutableArray arrayWithCapacity:1]; NSString *jsonStr = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://phobos.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/toppaidapplications/limit=75/json"] encoding:NSUTF8StringEncoding error:nil]; NSDictionary *dic = [jsonStr JSONValue];//通过json 从网络上获得数据 self.listTableViewArray = [[dic objectForKey:@"feed"]objectForKey:@"entry"]; table = [[[UITableView alloc]initWithFrame:CGRectMake(100, 100, 320, 480) ]autorelease]; [self.view addSubview:table]; table.backgroundColor = [UIColor yellowColor]; table.delegate = self; // 设置代理 table.dataSource = self;// 设置数据源 // Do any additional setup after loading the view, typically from a nib.}-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ {return 1;}}-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ {return [self.listTableViewArray count];//返回cell的函数 }}-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ {static NSString *identifier = @"ddddd"; UITableViewCell UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; if (cell == nil) { cell cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]autorelease]; } NSDictionary } NSDictionary *item = [self.listTableViewArray objectAtIndex:indexPath.row]; cell.textLabel.text cell.textLabel.text =[[item objectForKey:@"im:name"]objectForKey:@"label"]; NSString NSString *imageName = [[[[self.listTableViewArray objectAtIndex:indexPath.row] valueForKey:@"im:name"] valueForKey:@"label"] stringByAppendingString:@".temp"]; //先到Library/Caches/ 目录下找图片 如果没有 就使用默认图片 NSString //先到Library/Caches/ 目录下找图片 如果没有 就使用默认图片 NSString *imageDataPath = [NSHomeDirectory() stringByAppendingPathComponent:[@"Library/Caches/" stringByAppendingString:imageName]]; UIImage UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfFile:imageDataPath]]; if (image) { cell.imageView.image cell.imageView.image = image; } }else { cell.imageView.image cell.imageView.image = [UIImage imageNamed:@"Placeholder.png"]; //placeholder为在未加载完成加载图片时显示的默认图片 }return cell;}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
// 因为UITableView 继承的是UIScrollView 所以继承方法scrollViewDidEndDecelerating 这个方法是 拖拽 scroll之后 完成减速时执行
[self performSelectorInBackground:@selector(loadCellImage) withObject:nil]; //另开线程 执行加载cell里的图片
NSLog(@"scrollViewDidEndDecelerating");
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{//行将显示的时候调用
if (!tableView.isDragging && !tableView.isDecelerating)
{
[self performSelectorInBackground:@selector(loadCellImage) withObject:nil];
NSLog(@" table view willDisplayCell");
}
}
-(void)loadCellImage
{
NSArray *indexPathsForLoad = [table indexPathsForVisibleRows];// 获得 table 当前显示的cells
for (NSIndexPath *item in indexPathsForLoad) {
NSInteger rowNumberForCell = item.row;
NSString *imageStr = [[[[self.listTableViewArray objectAtIndex:rowNumberForCell]objectForKey:@"im:image"]objectAtIndex:0]objectForKey:@"label"];
NSString *imageName = [[[[self.listTableViewArray objectAtIndex:rowNumberForCell]valueForKey:@"im:name"]valueForKey:@"label"]stringByAppendingString:@".temp"];
NSString *imageDataPath = [NSHomeDirectory() stringByAppendingPathComponent:[@"Library/Caches/" stringByAppendingString:imageName]];
if (![[NSFileManager defaultManager]fileExistsAtPath:imageDataPath]) {//先判断Library/Caches/ 这个目录下有没有这个文件如果没有 就写入目录
NSURL *imageurl = [NSURL URLWithString:[imageStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData *imageData = [NSData dataWithContentsOfURL:imageurl];
[imageData writeToFile:imageDataPath atomically:YES];
UIImage *image = [UIImage imageWithData:imageData];
UITableViewCell *cell = [table cellForRowAtIndexPath:item];
cell.imageView.image = image;// 更新cell的 image
}
}
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{// 因为UITableView 继承的是UIScrollView 所以继承方法scrollViewDidEndDecelerating 这个方法是 拖拽 scroll之后 完成减速时执行 [self performSelectorInBackground:@selector(loadCellImage) withObject:nil]; //另开线程 执行加载cell里的图片 NSLog(@"scrollViewDidEndDecelerating");}-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{//行将显示的时候调用 if (!tableView.isDragging && !tableView.isDecelerating) { [self performSelectorInBackground:@selector(loadCellImage) withObject:nil]; NSLog(@" table view willDisplayCell"); } }-(void)loadCellImage{ NSArray *indexPathsForLoad = [table indexPathsForVisibleRows];// 获得 table 当前显示的cells for (NSIndexPath *item in indexPathsForLoad) { NSInteger rowNumberForCell = item.row; NSString *imageStr = [[[[self.listTableViewArray objectAtIndex:rowNumberForCell]objectForKey:@"im:image"]objectAtIndex:0]objectForKey:@"label"]; NSString *imageName = [[[[self.listTableViewArray objectAtIndex:rowNumberForCell]valueForKey:@"im:name"]valueForKey:@"label"]stringByAppendingString:@".temp"]; NSString *imageDataPath = [NSHomeDirectory() stringByAppendingPathComponent:[@"Library/Caches/" stringByAppendingString:imageName]]; if (![[NSFileManager defaultManager]fileExistsAtPath:imageDataPath]) {//先判断Library/Caches/ 这个目录下有没有这个文件如果没有 就写入目录 NSURL *imageurl = [NSURL URLWithString:[imageStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; NSData *imageData = [NSData dataWithContentsOfURL:imageurl]; [imageData writeToFile:imageDataPath atomically:YES]; UIImage *image = [UIImage imageWithData:imageData]; UITableViewCell *cell = [table cellForRowAtIndexPath:item]; cell.imageView.image = image;// 更新cell的 image }
}
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
// 因为UITableView 继承的是UIScrollView 所以继承方法scrollViewDidEndDecelerating 这个方法是 拖拽scroll之后 完成减速时执行
[self performSelectorInBackground:@selector(loadCellImage) withObject:nil]; //另开线程 执行加载cell里的图片
NSLog(@"scrollViewDidEndDecelerating");
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{//行将显示的时候调用
if (!tableView.isDragging && !tableView.isDecelerating)
{
[self performSelectorInBackground:@selector(loadCellImage) withObject:nil];
NSLog(@" table view willDisplayCell");
}
}
-(void)loadCellImage
{
NSArray *indexPathsForLoad = [table indexPathsForVisibleRows];// 获得 table 当前显示的cells
for (NSIndexPath *item in indexPathsForLoad) {
NSInteger rowNumberForCell = item.row;
NSString *imageStr = [[[[self.listTableViewArray objectAtIndex:rowNumberForCell]objectForKey:@"im:image"]objectAtIndex:0]objectForKey:@"label"];
NSString *imageName = [[[[self.listTableViewArray objectAtIndex:rowNumberForCell]valueForKey:@"im:name"]valueForKey:@"label"]stringByAppendingString:@".temp"];
NSString *imageDataPath = [NSHomeDirectory() stringByAppendingPathComponent:[@"Library/Caches/" stringByAppendingString:imageName]];
if (![[NSFileManager defaultManager]fileExistsAtPath:imageDataPath]) {//先判断Library/Caches/ 这个目录下有没有这个文件如果没有 就写入目录
NSURL *imageurl = [NSURL URLWithString:[imageStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData *imageData = [NSData dataWithContentsOfURL:imageurl];
[imageData writeToFile:imageDataPath atomically:YES];
UIImage *image = [UIImage imageWithData:imageData];
UITableViewCell *cell = [table cellForRowAtIndexPath:item];
cell.imageView.image = image;// 更新cell的 image
}
摘自http://www.cnblogs.com/gaoxiao228/archive/2012/05/28/2522683.html
0 0
- UITableView缓存加载图片
- UITableView异步加载图片+缓存
- UITableView 异步加载图片
- UITableView异步加载图片
- UITableView异步加载图片
- UITableView异步加载图片
- UITableView 异步加载图片
- iPhone UITableView异步加载图片
- iPhone UITableView异步加载图片
- ios uitableview加载图片优化
- 图片双缓存加载
- EGOImageView图片缓存加载
- 图片加载双缓存
- 网络加载图片缓存
- iPhone UITableView异步加载图片 (动态加载图片)
- UITableView 实现异步加载图片之改进
- uitableview自定义cell,cell加载图片
- UITableView NSThread 实现动态加载图片
- iOS数据存储
- ActionBar自定义背景
- Java注解
- Apache Hadoop 2.6.0 新特性
- 程序员的笑话/漫画集锦
- UITableView缓存加载图片
- SEO必知的100个网站优化问答(一)
- 作为程序员,这些地方不知道就out了!
- org.springframework.beans.factory.BeanCreationException: Error creating bean with name
- android驱动学习---led实验
- 偷盗者--Java流,文件和I/O
- php 起始页,验证php的web server是否已经成功 fedora linux+apache+mysql+php平台构建及环境配置
- sqlplus
- 刷题—Palindrome Number