【问题标题】:SDWebImage loading issue.!SDWebImage 加载问题!
【发布时间】:2014-09-09 05:17:24
【问题描述】:

您好,我正在使用 SDWebImage 将远程图像加载到我的集合视图中。 第一次它的加载很好,就像第一次显示占位符图像并且当图像被检索然后更改单元格的图像...但是第二次我加载相同的图像时,顶部可见行没有显示图像但是当我向下滚动到其他图像时,它们加载正常。然后我加载回顶部,图像就在那里。

在我的 collectionview 类中 -

- (void)startIconDownload:(TrialImages *)appRecord forIndexPath:(NSIndexPath *)indexPath
{
  TrialPicDownloader *iconDownloader = [_imageDownloadsInProgress objectForKey:indexPath];
  if (iconDownloader == nil)
  {
        iconDownloader = [[TrialPicDownloader alloc] init];
        iconDownloader.productRecord = appRecord;
        [iconDownloader setCompletionHandler:^{
              MyCollectionViewCell *cell = (MyCollectionViewCell *)[self.myCollectionView cellForItemAtIndexPath:indexPath];
              cell.trialImageView.image = appRecord.trialImage;

              [_imageDownloadsInProgress removeObjectForKey:indexPath];

        }];
        [_imageDownloadsInProgress setObject:iconDownloader forKey:indexPath];
        [iconDownloader startDownload];
  }
}

此方法在 TrialPicDownloader 类中 -

- (void)startDownload
{
  [[SDWebImageManager sharedManager] downloadWithURL:
[NSURL URLWithString:self.productRecord.TrialImagesUrl]
                                   options:SDWebImageCacheMemoryOnly
                                   progress:nil
                                   completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) {
                 if (image != NULL) {
                                 self.productRecord.trialImage = image;
                                 if (self.completionHandler)
                                    self.completionHandler();
                                      }
                                   }];


}

【问题讨论】:

    标签: ios objective-c sdwebimage


    【解决方案1】:

    没关系,我可以通过这段代码解决我的问题......

    - (void)startIconDownload:(TrialImages *)appRecord forIndexPath:(NSIndexPath *)indexPath
    {
      TrialPicDownloader *iconDownloader = [_imageDownloadsInProgress objectForKey:indexPath];
      if (iconDownloader == nil)
      {
            iconDownloader = [[TrialPicDownloader alloc] init];
            iconDownloader.productRecord = appRecord;
            [iconDownloader setCompletionHandler:^{
                  dispatch_async(dispatch_get_main_queue(), ^{
                        MyCollectionViewCell *cell = (MyCollectionViewCell *)[self.myCollectionView cellForItemAtIndexPath:indexPath];
                        cell.trialImageView.image = appRecord.trialImage;
                        [UIView animateWithDuration:0.3f animations:^{
                              [self.myCollectionView.collectionViewLayout invalidateLayout];
                        }];
                        [_imageDownloadsInProgress removeObjectForKey:indexPath];
                  });
            }];
            [_imageDownloadsInProgress setObject:iconDownloader forKey:indexPath];
            [iconDownloader startDownload];
      }
    }
    

    我使用 dispatch_async 处理主队列上的完成处理程序,现在它工作得很好..!!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-20
      • 1970-01-01
      • 2015-05-06
      • 1970-01-01
      • 1970-01-01
      • 2013-08-03
      • 1970-01-01
      相关资源
      最近更新 更多