0
votes

hello i am loading remote images into my collection view with SDWebImage. for the first time its loading fine like first showing placeholder image and when image is retreived then change the image of cell... but second time i load the same image the top visible rows are not showing image but when i scroll down to other images, they load fine. then i load back to top, images are there.

In my collectionview class -

- (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];
  }
}

this method is in TrialPicDownloader class -

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

1 Answers

0
votes

NeverMind i was able to solve my issue by this code ...

- (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];
  }
}

i used dispatch_async to process the completion handler on main queue and now its working great..!!