문제

In my method cellForItemAtIndexPath i have this code:

SDWebImageManager *manager = [SDWebImageManager sharedManager];
[manager downloadWithURL:[NSURL URLWithString:coverURL] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {

} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) {
    RRRBigItemCell *cell = (RRRBigItemCell *)[collectionView cellForItemAtIndexPath:indexPath];
    if (cell) {
        RRRImageView *coverView = (RRRImageView *)[cell viewWithTag:COVER_TAG];
        coverView.image = image;
    }
}];

When i launch application and i scroll down then everything is ok. But when i scroll up than cell which were already displayed don't have loaded images (cell == nil). And if i again scroll down the problem remains for already displayed cells, only new ones have loaded images. Any idea what i'm doing wrong?

도움이 되었습니까?

해결책

@pawan is correct, that would be simplest way to implement SDWebImage. It's basically a category of UIImage, so simply use the category methods (setImageWithURL).

A couple of other points since you asked;

  1. In your code, you should be setting the coverImage.image on the main thread, not in the background.

  2. It's good practice to check if the Cell is visible, or there is little point in displaying the content.

  3. It's good practice to create a weak reference to the Collection view when accessing the cell to avoid circular references

so your code ) might be written as follows if you were insistent on doing it the way you have it (Using the category is the best and simplest way)

  __weak myCollectionViewController *weakSelf = self;
  SDWebImageManager *manager = [SDWebImageManager sharedManager];
  [manager downloadWithURL:[NSURL URLWithString:coverURL] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {

  } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) {

     dispatch_async(dispatch_get_main_queue(), ^{
        if ([weakSelf.collectionView.indexPathsForVisibleItems containsObject:indexPath]) {
           RRRBigItemCell *cell =  (RRRBigItemCell *)[weakSelf.collectionView cellForItemAtIndexPath:indexPath];
           RRRImageView *coverView = (RRRImageView *)[cell viewWithTag:COVER_TAG];
           coverView.image = image;
        };
     });  

 }];

다른 팁

why dont you try this [cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

reference SDWebImage

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top