我目前有一个可以显示任意数量结果的 tableView。每行都有一个从服务器中提取的图像。假设服务器速度很快并且设备具有可靠的连接,我如何才能减少滚动列表时的延迟。
我查看了在后台线程上加载图像,这确实改善了一些事情,但是您可以在上下滚动时清楚地看到图像渲染。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if (tableView == self.searchDisplayController.searchResultsTableView)
{
cell.textLabel.text = [self.searchResult objectAtIndex:indexPath.row];
}
else
{
cell.textLabel.text = self.TableData[indexPath.row];
}
_ImageURL = self.ImageData[indexPath.row];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^
{
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:_ImageURL]]];
dispatch_sync(dispatch_get_main_queue(), ^
{
[[cell imageView] setImage:image];
[cell setNeedsLayout];
});
});
return cell;
}
是否有任何方法可以改善这一点,或者它更有可能是服务器速度和设备带宽的限制。我的服务器不是 Facebook 服务器,但它是我用于其他应用程序的付费服务器。
此外,自从将图像移至后台任务后,我注意到时不时有一行会错过加载图像,对此有什么想法吗?
最佳答案
我推荐你使用 SDWebImage
库,一切都会为你处理,从异步下载到缓存管理。
这是 the link用于下载和文档。
您还可以使用 Facebook 的 AsyncDisplayKit 如果您不需要图像缓存。
祝你好运。
关于ios - 为 Tableview 使用服务器端图像时减少滚动延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33112805/