【问题标题】:Table view scroll lagging when using SDWebImage使用 SDWebImage 时表格视图滚动滞后
【发布时间】:2014-06-17 21:43:08
【问题描述】:

我正在cellForRowAtIndexPath 内的表格视图中从 Internet 加载一些图像。这是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *MyIdentifier = @"ArticleCell";
    ArticleCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    Article *article = [parser items][indexPath.row];

    cell.title.text = article.title;
    cell.newsDescription.text = article.description;
    [cell.image setImageWithURL:[NSURL URLWithString:article.image]];

    return cell;
}

我的问题是即使我使用SDWebImage,当我向下滚动时,我的应用程序仍然滞后。这是Instruments的一些截图:

【问题讨论】:

  • 图片有多大?
  • 嗯,介于 50.000 和 200.000 之间,比如说 150 KB
  • 图片是什么类型的? TIFF?
  • 你解决了这个问题?
  • @Nikita:我的问题是我在主线程上下载图像后对图像做了更多的工作(使图片循环),接受的答案对你有用吗?

标签: ios sdwebimage


【解决方案1】:

看起来即使图像的下载是在后台线程中执行的,但图像数据的工作是在主线程中完成的,因此它会阻塞您的应用程序。你可以试试 SDWebImage 提供的异步图片下载器。

[SDWebImageDownloader.sharedDownloader downloadImageWithURL:imageURL
                                                    options:0
                                                   progress:^(NSUInteger receivedSize, long long expectedSize)
                                                   {
                                                       // progression tracking code
                                                   }
                                                   completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished)
                                                   {
                                                       if (image && finished)
                                                       {
                                                           // do something with image
                                                       }
                                                   }
];

在你的方法中应该是这样的:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *MyIdentifier = @"ArticleCell";
    ArticleCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    Article *article = [parser items][indexPath.row];

    cell.title.text = article.title;
    cell.tag = indexPath.row;
    cell.newsDescription.text = article.description;
    [SDWebImageDownloader.sharedDownloader downloadImageWithURL:imageURL
                                                        options:0
                                                       progress:nil
                                                      completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished)
     {
         if (cell.tag == indexPath.row && image && finished)
         {
            dispatch_async(dispatch_get_main_queue(), ^(){
               cell.image = image;
            });

         }
     }];

    return cell;
}

【讨论】:

  • 1) 它不跟踪图像视图,因此不会在滚动时将图像放在正确的图像视图上。 2)图像在滚动时消失,并在一段时间后再次出现。似乎没有使用缓存
  • 关于2号,SDWebImageDownloader默认缓存图片。
  • 是的,但是即使图像来自缓存,也不应该错误地放置图像!自己检查您的代码! 2)问题仍然存在!
【解决方案2】:

在单独的线程上下载图像,如下所示:

NSURLRequest *request = [NSURLRequest requestWithURL:yourURL];
                        [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
                            if ( data )
                            {
                                UIImage *image = [[UIImage alloc] initWithData:data];
                                [cell.image setImage:image];
                            }
                        }];

【讨论】:

  • @vBx 说他已经使用了使用单独线程的 sdWebImage 扩展。
【解决方案3】:

这可能与网络活动的关系不大,而与您的图像大小有关。调整图像大小,特别是对于非整数乘法器,代价高昂。如果您的图像比您放入的视图大得多,您需要在后台线程中调整和缓存它们,并从您的视图中引用缓存的副本。

要确认这是否是您的问题,请手动下载所有图像并参考本地副本。如果我是正确的,你的 UITableView 仍然会以同样的方式滞后。

【讨论】:

    【解决方案4】:

    你必须从服务器异步加载,这样你的 tableView 才能平滑滚动。

    请检查以下答案,您将能够解决您的问题。

    https://stackoverflow.com/a/15331306/1713478

    【讨论】:

      【解决方案5】:

      检查您的图像,检查类型和大小以及更重要的:一张或多张图像是否被其文件格式以任何方式破坏?文件大小太大,不规则(到大)边界?

      尝试使用图像编辑器打开并重新保存可疑图像文件,以确保内部文件格式正常。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-30
        • 2021-05-11
        • 1970-01-01
        相关资源
        最近更新 更多