Get SDWebImage Cache Image

混江龙づ霸主 提交于 2019-12-04 17:35:27

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.

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!

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];
    }];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!