IOS之tableview 实现动态加载图片
2013-05-14 18:41
567 查看
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;
}
- (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学习之路 TableView实现动态加载图片
- iOS开发使用sdWebImage实现对tableView的cell加载图片淡入动画
- iPhone table 实现动态加载图片的教程-TableView中图片的延时加载
- iOS---tableview加载图片的时候的优化之lazy(懒加载)模式and异步加载模式
- iOS开发中如何解决TableView中图片延时加载
- iOS tableview SDWebImage加载多张图片内存警告(received memory warning)
- iOS程序开发之使用PlaceholderImageView实现优雅的图片加载效果
- UITableView NSThread 实现动态加载图片
- ios实现tableView顶部弹簧图片效果
- ios TableView那些事(三十 五)TableView 单选操作使用Autolayout实现UITableView的Cell动态布局和高度动态改变
- IOS中UITableView异步加载图片的实现
- IOS开发中如何解决TableView中图片延时加载
- IOS TableView远程图片加载笔记
- IOS(swift)-scrollView(tableView) ·图片加载逻辑的优化
- ios 异步加载图片结合tableView SDWebImage
- iOS开发中如何解决TableView中图片延时加载
- iPhone table 实现动态加载图片的教程
- iphone table 实现动态加载图片
- [iOS] 延时加载TableView中Cell中的图片
- iOS下拉tableView实现上面的图片放大效果