iPhone学习之路 Table实现动态加载图片

移动开发 iOS
本文介绍的是iPhone学习之路 Table实现动态加载图片,主要讲述的是对图片的操作,我们来看内容。

iPhone学习之路 Table实现动态加载图片是本文要介绍的内容,内容不多,基于代码实现,我们一起来看内容。

Iphone在加载列表时,如果每个等待把所有列表中的数据都加载完在显示相关内容,如果列表中有一些比较大的图片,加载的时间比较长,那么给用户的效果就很差了,下面详细是一种实现动态加载图片的办法:

  1. - (UITableViewCell *)tableView:(UITableView *)tableView   
  2.          cellForRowAtIndexPath:(NSIndexPath *)indexPath {   
  3.     UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"tag"];   
  4.     if (cell==nil) {   
  5.         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle   
  6.                                        reuseIdentifier:@"tag"] autorelease];   
  7.     }      
  8.     //表格设计   
  9.     NSDictionary* one = [array objectAtIndex:indexPath.row];   
  10.     cell.textLabel.text = [one objectForKey:@"title"];   
  11.     cell.detailTextLabel.text = [one objectForKey:@"content"];   
  12.      [NSThread detachNewThreadSelector:@selector(updateImageForCellAtIndexPath:) toTarget:self withObject:indexPath];   
  13.     return cell;   
  14. }   
  15.  
  16. - (void)updateImageForCellAtIndexPath:(NSIndexPath *)indexPath   
  17. {   
  18.     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];   
  19.     UIImage *image = [self getImageForCellAtIndexPath:indexPath];   
  20.     UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];   
  21.     [cell.imageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:NO];   
  22.     [image release];   
  23.     [pool release];   
  24. }   
  25. -(UIImage *)getImageForCellAtIndexPath:(NSIndexPath *)indexPath   
  26. {   
  27.     id path = [[array objectAtIndex:indexPath.row] objectForKey:@"image"];   
  28.     NSURL *url = [NSURL URLWithString:path];   
  29.     NSData *data = [NSData dataWithContentsOfURL:url];   
  30.     UIImage *image = [[UIImage alloc] initWithData:data cache:NO];   
  31.     return image;   

源代码:http://easymorse-iphone.googlecode.com/svn/trunk/WelcomePavilion/

小结:iPhone学习之路 Table实现动态加载图片的内容介绍完了,希望本文对你有所帮助!

本文来自:http://wangjun.easymorse.com/?p=908

责任编辑:zhaolei 来源: 互联网
相关推荐

2011-07-25 15:32:06

iPhone Table 动态

2011-07-06 16:15:46

iPhone 图片

2011-07-26 09:32:08

iPhone 模拟器

2023-10-10 15:32:09

veImageXWeb 图片

2023-04-25 17:24:31

veImageXiOSSDK

2013-06-27 11:16:27

Android异步加载

2011-07-25 16:47:01

HTTP Server PUSH

2011-07-22 13:30:52

JavaScript

2011-07-08 15:08:16

iPhone 图片

2009-08-28 16:14:26

C#实现加载动态库

2011-08-10 16:44:56

iPhone代理设计模式

2011-07-21 17:29:42

iPhone Sqlite 数据库

2011-07-27 17:07:06

iPhone 游戏 Cocos2d

2021-10-18 12:04:22

Spring BootJava开发

2021-10-18 10:36:31

Spring Boot插件Jar

2022-09-23 15:01:33

图片加载代码

2011-08-19 11:10:31

iPhone应用

2010-01-27 14:39:58

Android图片大小

2011-08-18 16:03:34

iPhone上传图片

2011-08-02 14:23:09

iPhone UIScrollVi 图片
点赞
收藏

51CTO技术栈公众号