SDWebImage加载问题。!

时间:2014-09-08 20:28:09

标签: ios objective-c sdwebimage

您好我正在使用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();
                                      }
                                   }];


}

1 个答案:

答案 0 :(得分:0)

NeverMind我能够通过此代码解决我的问题...

- (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处理主队列上的完成处理程序,现在它工作得很好.. !!