2013-10-13 43 views
7

我从cellForRowAtIndexPath里面的表格视图中加载了一些来自互联网的图片。这里是我的代码:使用SDWebImage时表格视图滚动滞后

- (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

enter image description here

enter image description here

+0

图像有多大? – Wain

+0

好吧,介于50.000和200.000之间,可以说150 KB – Kobe

+0

图像是什么类型? TIFF? – Wain

回答

5

它看起来像即使下载的图像在后台线程中执行,图像数据的工作是在主线程中完成,因此它会阻止您的应用程序。您可以尝试由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; 
} 
+0

1)它不跟踪图像视图,因此没有将图像放在滚动上的正确图像上。 2)图像在滚动时出现,并在一段时间后再次出现。 似乎没有使用缓存 – Developer

+0

@Developer我编辑修复1. –

+0

关于数字2,SDWebImageDownloader默认缓存图像。 –

1

下载一个单独的线程的形象,就像这样:

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]; 
          } 
         }]; 
+0

@vBx说他已经使用了sdWebImage扩展,它使用了一个分离的线程。 – ThorstenC

0

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

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

1

这可能有少做的网络活动,并更多地与你的尺寸的影像。调整图像大小,特别是对于非整数倍数,是昂贵的。如果您的图像比您放入的视图大得多,则需要调整大小并将其缓存在后台线程中,并从视图中引用缓存的副本。

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