使用Tableview的服务器端映像时减少滚动延迟

时间:2015-10-13 21:18:40

标签: 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服务器,但它是一个体面的付费服务器,我用于其他应用程序。

此外,自从将图像移动到后台任务后,我注意到一行又一次会错过加载图像,对此有什么想法?

1 个答案:

答案 0 :(得分:3)

我建议您使用SDWebImage库,一切都将为您处理,从异步下载到缓存管理。 以下是 the link下载和文档。

如果您不需要图像缓存,也可以使用Facebook的 AsyncDisplayKit

祝你好运。

相关问题