3

I'm using UICollectionView.

Images are coming from FTP server. Here is my code for downloading and Showing image on button :

[tempp sd_setImageWithURL:[NSURL URLWithString:[[arrFilterData objectAtIndex:indexPath.row] valueForKey:@"ZoomPrdImg"]] //PrdImg //ZoomPrdImg
             placeholderImage:nil
                    completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                        if (image) {
                            myCell.image.userInteractionEnabled = YES;
                            dispatch_async(dispatch_get_main_queue(), ^{
                                [myCell.image setImage:image forState:UIControlStateNormal];
                                [myCell setNeedsLayout];
                            });
                        }
                    }];

myCell.image is my button that set downloaded image. Now I don't get that image is successfully dowloaded but nut show in button. There's 3000+ images in UICollectionView but it shows some image and some blank.

How this possible ? How to solve this ? What is issue there ?

EDIT: tempp is allocted in viewDidLoad

- (void)viewDidLoad {
    tempp = [[UIImageView alloc] init];
}

2 Answers2

0

replace

 [myCell.image setImage:tempp.image forState:UIControlStateNormal];
                            [myCell setNeedsLayout];

with

 [myCell.image setImage:image forState:UIControlStateNormal];
                            [myCell setNeedsLayout];

Because in your completion handler you are getting your image in image object

Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
0

I think your variable tempp is destructed, and cancels request for the image. Change request code to

[myButton sd_setImageWithURL:[NSURL URLWithString:@"MyURL"] forState:UIControlStateNormal completed:yourCompletitionBlock];

And all should be fine.

UPD

Based on your answer about tempp variable, I think you see one Image per page, because when you call sd_setImage... for many cells, only last request is loading. Requests are asynchronous and if you request new image, old request is cancelled.

Code from SDWebImage:

- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletionBlock)completedBlock {
    [self sd_cancelCurrentImageLoad];
    objc_setAssociatedObject(self, &imageURLKey, url, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

First line cancels old request.

Andrew Romanov
  • 4,774
  • 3
  • 25
  • 40