iPhone table 实现动态加载图片的教程
2012-03-23 13:42
411 查看
from:http://www.cocoachina.com/bbs/read.php?tid=92444
iPhone在加载列表时,如果每个等待把所有列表中的数据都加载完在显示相关内容,如果列表中有一些比较大的图片,加载的时间比较长,那么给用户的效果就很差了,下面详细是一种实现动态加载图片的办法:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"tag"];
if (cell==nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:@"tag"] autorelease];
}
//表格设计
NSDictionary* one = [array objectAtIndex:indexPath.row];
cell.textLabel.text = [one objectForKey:@"title"];
cell.detailTextLabel.text = [one objectForKey:@"content"];
[NSThread detachNewThreadSelector:@selector(updateImageForCellAtIndexPath:) toTarget:self withObject:indexPath];
return cell;
}
- (void)updateImageForCellAtIndexPath:(NSIndexPath *)indexPath
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
UIImage *image = [self getImageForCellAtIndexPath:indexPath];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
[cell.imageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:NO];
[image release];
[pool release];
}
-(UIImage *)getImageForCellAtIndexPath:(NSIndexPath *)indexPath
{
id path = [[array objectAtIndex:indexPath.row] objectForKey:@"image"];
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [[UIImage alloc] initWithData:data cache:NO];
return image;
}
iPhone在加载列表时,如果每个等待把所有列表中的数据都加载完在显示相关内容,如果列表中有一些比较大的图片,加载的时间比较长,那么给用户的效果就很差了,下面详细是一种实现动态加载图片的办法:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"tag"];
if (cell==nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:@"tag"] autorelease];
}
//表格设计
NSDictionary* one = [array objectAtIndex:indexPath.row];
cell.textLabel.text = [one objectForKey:@"title"];
cell.detailTextLabel.text = [one objectForKey:@"content"];
[NSThread detachNewThreadSelector:@selector(updateImageForCellAtIndexPath:) toTarget:self withObject:indexPath];
return cell;
}
- (void)updateImageForCellAtIndexPath:(NSIndexPath *)indexPath
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
UIImage *image = [self getImageForCellAtIndexPath:indexPath];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
[cell.imageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:NO];
[image release];
[pool release];
}
-(UIImage *)getImageForCellAtIndexPath:(NSIndexPath *)indexPath
{
id path = [[array objectAtIndex:indexPath.row] objectForKey:@"image"];
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [[UIImage alloc] initWithData:data cache:NO];
return image;
}
相关文章推荐
- iPhone table 实现动态加载图片的教程
- iPhone table 实现动态加载图片的教程-TableView中图片的延时加载
- iPhone table 实现动态加载图片的教程
- iPhone table 实现动态加载图片的教程
- iphone table 实现动态加载图片
- iphone table 实现动态加载图片
- iPhone学习之路 TableView实现动态加载图片
- iphone table 实现动态加载图片
- iPhone table 实现动态加载图片的教程
- IOS之tableview 实现动态加载图片
- jQuery页面滚动图片等元素动态加载实现
- iPhone利用线程实现数据的加载,并展示在table列表中
- Qt 使用QMovie加载gif图片实现动态等待窗口
- 动态的把数据显示在table中,点击某一行的删除按钮实现该行的删除(通过ajax实现页面的无刷新加载)
- [unity基础教程]Unity3D实现动态加载游戏资源(转)
- Android图片加载神器之Fresco-圆形圆角二合一教程[Java代码实现圆形圆角效果]
- jQuery页面滚动图片等元素动态加载实现
- 用src属性动态替换图片;图片预加载---鼠标事件实现图片翻转效果;随机显示图片和onClick事件
- iphone(UITableViewCell)动态加载图片
- 以GIF为背景加载图片实现动态倒计时