ios - 为 Tableview 使用服务器端图像时减少滚动延迟

标签 ios objective-c xcode uitableview

我目前有一个可以显示任意数量结果的 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/

相关文章:

ios - 适用于 iPhone 和 iPad 的单 Nib

iphone - 你如何显示 Xcode 构建日志? (尝试验证 iPhone 发行版构建 zip 是否已正确创建。)

java - 无法提交申请

ios - 使用带有 UIViewController 容器的 UISearchDisplayController

ios - 如何在 Swift 中打印 NSTimeInterval?

ios - 如何在 Swift 中传入一个 nil 通知变量

ios - NSLayoutConstraint 以编程方式设置 View 框架

ios - 在 AFNetworking 的 JSONRequestOperationWithRequest :success:failure: method, 中,JSON 响应是什么类型的对象?

objective-c - 使用自动布局约束将工具栏项目对齐以 Split View内容

objective-c - Xcode 错误地指出 "Cloud.h"不存在