【问题标题】:iOS: UITableView lagging with image dataiOS:UITableView 滞后于图像数据
【发布时间】:2014-03-19 00:32:50
【问题描述】:

我有一个 UITableview,它有一个像这样的图像列表。但是由于某种原因,当我上下滚动时它非常滞后。有什么办法可以阻止这一切?图像不需要重新加载。它需要保持静止。

- (UITableViewCell *)tableView:(UITableView *)tableView
     cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = @"MyIdentifier";

UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:MyIdentifier];

if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                  reuseIdentifier:MyIdentifier];
}
ContentModel *contentModel = [self.tableArray objectAtIndex:indexPath.row];
NSURL *url = [NSURL URLWithString:contentModel.txtImages];
NSData *data = [NSData dataWithContentsOfURL:url];
cell.imageView.image = [[UIImage alloc] initWithData:data];

return cell;
 }

【问题讨论】:

    标签: ios objective-c uitableview uiimage nsdata


    【解决方案1】:

    你最好使用 AFNetworking UIImageView 方法:

    [imageView setImageWithURL:[NSURL URLWithString:@"http://image.com/image.png"]];
    

    即使有占位符:

    [self.image setImageWithURL:[NSURL URLWithString:@"http://image.com/image.png"] placeholderImage:     [UIImage imageNamed:@"logo"]];
    

    https://github.com/AFNetworking/AFNetworking

    这将大大减少您的 tableview 的延迟。

    【讨论】:

      【解决方案2】:

      我们有非常相似的要求,但我的代码完美运行,我猜是 这些命令会减慢您的日常工作:

      NSURL *url = [NSURL URLWithString:contentModel.txtImages];
      NSData *data = [NSData dataWithContentsOfURL:url];
      cell.imageView.image = [[UIImage alloc] initWithData:data];
      

      相反,我使用以下内容:

          NSString *icon = CurrentQRCode.parsed_icon;
        NSString *filePath = [[NSBundle mainBundle] pathForResource:icon ofType:@"png"]; 
          UIImage *image_file = [UIImage imageWithContentsOfFile:filePath];
          cell.imageView.image = image_file;
      

      整个程序如下:

      QRTypeCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
      if (cell == nil) {
          cell = [[QRTypeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];}
      
      
          CurrentQRCode = (QRCode *)[fetchedResultsController objectAtIndexPath:indexPath];
          NSString *icon = CurrentQRCode.parsed_icon;
          NSString *string = CurrentQRCode.parsed_string;
          cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
          NSString *filePath = [[NSBundle mainBundle] pathForResource:icon ofType:@"png"]; 
          UIImage *image_file = [UIImage imageWithContentsOfFile:filePath];
          cell.imageView.image = image_file;
          cell.labelview.text = string;
      

      我相信我最初尝试使用 initWithData 处理 UIImage,但发现它们比其他方法慢很多。

      【讨论】:

      • Paulo,您正在本地加载资源,而不是通过 URL 加载海报。大不同。
      • 我的是本地的,如果您需要从网络访问图像-也许您应该尝试@user3290977的建议,看看是否会更好,否则我建议您下载所有图标在创建数据源的同时生成一个 tmp 文件
      • 一次下载所有图标的原因是什么?这是假设海报知道所有图像。我不认为一次下载所有图像是正确的方法。但也许你有它的理由?
      • 您不希望屏幕显示/或启动依赖于可能无法始终可用的东西。对于大多数移动用户来说,网络连接变化无常,上电梯就消失了。你的数据源是一个数组这一事实让我怀疑你的数据不是绝对实时的,我认为这是你可以做的事情来提高你的应用程序的可用性。
      • 我刚刚意识到你不是海报 - 你的 cmets 怎么了?没有足够的信息来确定应用程序正在使用什么样的数据,使用的命令可以引用本地和网络数据。数据源的其余部分是一个数组这一事实可能意味着数据不是绝对实时的。在这种情况下,每次加载/重新加载 tableview 时,最好执行一次而不是多次。
      【解决方案3】:

      永远不要在主线程上从网络加载数据。使用 GCD 或 AFNetworking 或 restkit 等框架。你可能对你的应用程序做的最糟糕的事情就是用可以从主线程计算出来的东西锁定你的主线程。一个好的经验法则是询问这是否正在更新 UI,如果不考虑将其移动到另一个线程。

      如果您真的想了解其中的原因,我还会查看一些关于多线程的 WWDC 视频以及与脱离主线程相关的任何内容。

      AFNetworking

      RestKit

      GCD

      【讨论】:

        猜你喜欢
        • 2013-06-16
        • 1970-01-01
        • 1970-01-01
        • 2019-10-02
        • 1970-01-01
        • 2012-05-20
        • 1970-01-01
        • 2010-12-11
        • 1970-01-01
        相关资源
        最近更新 更多