TableView中图片的延时加载
来源:互联网 发布:mac水漾轻盈粉底液色号 编辑:程序博客网 时间:2024/04/28 12:09
关于TableView中图片的延时加载
经常我们会用tableView显示很多条目, 有时候需要显示图片, 但是一次从服务器上取来所有图片对用户来浪费流量, 对服务器也是负担.最好是按需加载,即当该用户要浏览该条目时再去加载它的图片.
重写如下方法
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
UIImage *image = [self getImageForCellAtIndexPath:indexPath]; //从网上取得图片
[cell.imageView setImage:image];
}
这虽然解决了延时加载的问题, 但当网速很慢, 或者图片很大时(假设,虽然一般cell中的图很小),你会发现程序可能会失去对用户的响应.
原因是UIImage *image = [self getImageForCellAtIndexPath:indexPath]; 这个方法可能要花费大量的时间,主线程要处理这个method.
所以失去了对用户的响应.
所以要将该方法提出来:
- (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];
[pool release];
}
然后再新开一个线程去做这件事情
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
[NSThread detachNewThreadSelector:@selector(updateImageForCellAtIndexPath:) toTarget:self withObject:indexPath];
}
同理当我们需要长时间的计算时,也要新开一个线程去做这个计算以避免程序处于假死状态
以上代码只是示例, 还可以改进的更多, 比如从网上down下来一次后就将图片缓存起来,再次显示的时候就不用去下载.
重写如下方法
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
UIImage *image = [self getImageForCellAtIndexPath:indexPath]; //从网上取得图片
[cell.imageView setImage:image];
}
这虽然解决了延时加载的问题, 但当网速很慢, 或者图片很大时(假设,虽然一般cell中的图很小),你会发现程序可能会失去对用户的响应.
原因是UIImage *image = [self getImageForCellAtIndexPath:indexPath]; 这个方法可能要花费大量的时间,主线程要处理这个method.
所以失去了对用户的响应.
所以要将该方法提出来:
- (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];
[pool release];
}
然后再新开一个线程去做这件事情
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
[NSThread detachNewThreadSelector:@selector(updateImageForCellAtIndexPath:) toTarget:self withObject:indexPath];
}
同理当我们需要长时间的计算时,也要新开一个线程去做这个计算以避免程序处于假死状态
以上代码只是示例, 还可以改进的更多, 比如从网上down下来一次后就将图片缓存起来,再次显示的时候就不用去下载.
- 关于TableView中图片的延时加载
- TableView中图片的延时加载
- iPhone TableView中图片的延时加载
- 关于TableView中图片的延时加载
- 关于TableView中图片的延时加载
- iPhone TableView中图片的延时加载
- iPhone TableView中图片的延时加载
- 关于TableView中图片的延时加载
- 关于TableView中图片的延时加载
- 关于TableView中图片的延时加载
- TableView中图片的延时加载
- 关于TableView中图片的延时加载
- TableView中图片延时加载
- iPhone table 实现动态加载图片的教程-TableView中图片的延时加载
- iOS开发中如何解决TableView中图片延时加载
- iOS开发中如何解决TableView中图片延时加载
- iOS开发中如何解决TableView中图片延时加载
- [iOS] 延时加载TableView中Cell中的图片
- hdoj 1030
- 二、扣篮那些事儿
- des解密
- [未AC]HDU1002
- ShellExecute与ShellExecuteEx的用法
- TableView中图片的延时加载
- 研究发现适量喝啤酒可增进骨骼健康
- HTTP状态码
- 一个老公写给老婆的年终总结
- 捕获CException异常
- 您的帐户被限制发帖
- 2009年最后一天总结
- WinForm GridView导出Excel
- jetty日志转换为log4j