Get SDWebImage Cache Image

2020-07-23 08:02发布

I would like to ask on how to get the downloaded image after the SDWebImageManager downloaded it. I have only the code to download it via URL, here's what I got:

let manager: SDWebImageManager = SDWebImageManager.sharedManager()
                        manager.downloadImageWithURL(NSURL(string: feedDetails.thumbnail), options: [],
                            progress: {(receivedSize: Int, expectedSize: Int) -> Void in
                                print(receivedSize)
                            },
                            completed: {(image, error, cached, finished, url) -> Void in

                                self.feedImage.image = image
                            }
                        )

3条回答
\"骚年 ilove
2楼-- · 2020-07-23 08:46

From the SDWebImageManager class the downloadImageWithURL: method

Downloads the image at the given URL if not present in cache or return the cached version otherwise.

So if the image is present in cache you are already retrieving it with your code, instead of downloading from the web.

查看更多
虎瘦雄心在
3楼-- · 2020-07-23 08:56

As far as I know (I just looked up the author's Git page) there is the following method to directly access an image which is stored inside the cache-

You can use the SDImageCache to store an image explicitly to the cache with the following code:

[[SDImageCache sharedImageCache] storeImage:myImage forKey:myCacheKey];

Where myImage is the image you want to store and myCacheKey is a unique identifier for the image.

After you stored an image to the cache and want to use that image, just do the following:

[[SDImageCache sharedImageCache] queryDiskCacheForKey:myCacheKey done:^(UIImage *image) {
    // image is not nil if image was found
}];

This code is Objective-C code, you have to "convert" it to swift yourself.

I hope I could help you!

查看更多
做个烂人
4楼-- · 2020-07-23 08:59

Thanks for answer @beeef but SDWebImage has been updated some part of code: Save image:

 [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:string] options:SDWebImageDownloaderUseNSURLCache progress:nil completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {

        if (image && finished) {
            // Cache image to disk or memory

            [[SDImageCache sharedImageCache] storeImage:image forKey:@"img_key" toDisk:YES completion:^{
                //save
            }];
        }
    }];

Get image from disk cache:

 [[SDImageCache sharedImageCache] queryCacheOperationForKey:@"img_key" done:^(UIImage * _Nullable image, NSData * _Nullable data, SDImageCacheType cacheType) {
        [self.imageV setImage: image];
    }];
查看更多
登录 后发表回答