如何进行multithreading加载图像更快到tableView?

我正在将图像加载到tableView,每次都是函数

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

执行,图像的不同url将来..

我的问题是它花了这么多时间来加载图像..

我的代码是……

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { self.tableView1 = tableView; static NSString *simpleTableIdentifier = @"SimpleTableCell"; SimpleTableCell *cell = (SimpleTableCell *)[tableView1 dequeueReusableCellWithIdentifier:simpleTableIdentifier]; if (cell == nil) { NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil]; cell = [nib objectAtIndex:0]; } cell.nameLabel.text = title; NSString *imageURL = [NSString stringWithFormat: @"http://sofzh.miximages.com/ios/]; cell.thumbnailImageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:fullURL]]]; } return cell; } 

每次图像url都会改变,加载每个图像需要一些时间。

任何人都可以提出解决这个问题的想法吗? multithreading如何使用此代码? 应该在代码中编辑的位置和内容?

你必须在后台下载图像,如:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *simpleTableIdentifier = @"SimpleTableCell"; SimpleTableCell *cell = (SimpleTableCell *)[tableView1 dequeueReusableCellWithIdentifier:simpleTableIdentifier]; dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul); dispatch_async(queue, ^{ NSString *u=[NSString stringWithContentsOfFile:r.url encoding:NSUTF8StringEncoding error:nil]; NSURL *imageURL=[NSURL URLWithString:u]; NSData *imageData=[NSData dataWithContentsOfURL:imageURL]; dispatch_sync(dispatch_get_main_queue(), ^{ SimpleTableCell *cell = (SimpleTableCell *)[tableView cellForRowAtIndexPath:indexPath]; cell.thumbnailImageView.image=[UIImage imageWithData:imageData]; [cell setNeedsLayout]; }); }); } return cell; } 

只需在表格视图方法中使用上面的代码并进行必要的编辑即可。

您不需要multithreading。 至少你不需要自己做。

看看这个教程。 当我开始使用Apps时,这对我非常有帮助。 http://www.markj.net/iphone-asynchronous-table-image/

或者子类UIImageView并执行此操作: http : //codeshape.wordpress.com/2011/05/10/creating-a-lazy-loading-uiimageview-for-ios/

看一下SDWebImage框架:

SDWebImage框架

也许它会帮助你。

我之前使用过Jeremy的答案的替代方案是HJCache框架

可以只使用HJManagedImage对象,它将为您异步下载和缓存图像