简体   繁体   English

如何知道图像是来自SDWebImage中的缓存还是表单URL?

[英]How know whether image is coming from cache or form url in SDWebImage?

I am using SDWebImage for loading a list of images and it was really good but while loading images next time it's loading image from cache if the url is same. 我正在使用SDWebImage来加载图像列表,这非常好,但是下次加载图像时,如果网址是相同的,则从缓存加载图像。 But I need to know its coming form cache or web. 但我需要知道它即将到来的表格缓存或网络。 Any help would be greatly appreciated. 任何帮助将不胜感激。

Check SDWebImageManager, it has 2 versions of functions to check whether an URL is already cached or not: 检查SDWebImageManager,它有2个版本的函数来检查URL是否已经被缓存:

- (BOOL)diskImageExistsForURL:(NSURL *)url;

There's also a version with a block which executes after the check. 还有一个带有块的版本,它在检查后执行。 Hope this helps! 希望这可以帮助!

You can check the cache to see if the image in question has already been downloaded like so: 您可以检查缓存以查看相关图像是否已经下载,如下所示:

NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:URL];
UIImage *image = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:key];

use your url 使用你的网址

SDWebImageManager *manager = [SDWebImageManager sharedManager];
UIImage *cachedImage = [manager imageWithURL:url];

if (cachedImage) 
{
   [button setImage:cachedImage];
   //image is cashed
}
else 
{
   [manager downloadWithURL:url delegate:self];
}

and then implement delegate function 然后实现委托功能

- (void)webImageManager:(SDWebImageManager *)imageManager didFinishWithImage:(UIImage*)image {
   [button setImage:image];
   // new image
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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