Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SDWebImage eager load images

I am trying to eager load bunch of images:

for (NSDictionary *s in things) {
    [manager downloadWithURL:[NSURL URLWithString:s[photo]]
                     options:0
                    progress:nil
                   completed:nil];
}

It's not downloading these images. However, if I pass in an empty completion block, like so:

for (NSDictionary *s in things) {
    [manager downloadWithURL:[NSURL URLWithString:s[photo]]
                     options:0
                    progress:nil
                   completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) { }];
}

then it works just fine. My question is: why? Is there a better way to do this? Passing in an empty block doesn't seem right to me.

like image 729
0xSina Avatar asked Aug 29 '13 11:08

0xSina


1 Answers

The API you are using is not the correct one.

To prefetch images and store them in cache, use SDWebImagePrefetcher which is meant for that.

NSMutableArray * urls = [NSMutableArray arrayWithCapacity:things.count];
for (NSDictionary *s in things) {
    [urls addObject:[NSURL URLWithString:s[photo]]];
}
[[SDWebImagePrefetcher sharedImagePrefetcher] prefetchURLs:urls];

As a side note I submitted a pull request - which has just been merged - to enforce the presence of a completedBlock in the API you are (mis)using, so that other programmers won't fall you in your same mistake.

like image 157
Gabriele Petronella Avatar answered Oct 10 '22 07:10

Gabriele Petronella