简体   繁体   中英

Use SDWebImage to download two images at once

I'm looking into using SDWebImage to download images into my app. I would like to show an image as quickly as possible, so I have two versions of each image on my server; one low quality and one high quality. Currently, I achieve this by using two NSURLRequest , one for each version of the image. This works perfectly as the app will download both versions simultaneously, show the low quality, then change the image to the high quality version when it's finished downloading.

I tried to replicate this by using:

[imageView setImageWithURL:lowURL];
[imageView setImageWithURL:highURL];

But when I run the app, the second call cancels the first one. Any help with this would be greatly appriciated.

Thanks

modify framework is not a good way to solve problem, unless there is no other better way.

in SDWebImage's UIImageView+WebCache.h category, there are methods for this situation.

eg:

__weak UIImageView *weakImageView = imageView;
[imageView setImageWithURL:thumbURL placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
    [weakImageView setImageWithURL:bigURL placeholderImage:image];
}];

If you look into the - (void)setImageWithURL:(NSURL *)url in the UIImageView+WebCache.m file, you will find there is [self cancelCurrentImageLoad]; in the first line, which is cancel the current image loading operation. if you want to load both simultaneously, you can try to comment out this line to test whether it will work as you expected, if cannot then you'd better try other methods.

probably this issue is better being solved in a different way, try pre-fetching the image even before displaying it

this is the swift code to do so

let prefetcher = SDWebImagePrefetcher.shared()
let urls :[URL] = [URL(string: imageStringUrl)] 
prefetcher.prefetchURLs(urls)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM