简体   繁体   English

获取SDWebImage缓存图像

[英]Get SDWebImage Cache Image

I would like to ask on how to get the downloaded image after the SDWebImageManager downloaded it. 我想问一下SDWebImageManager下载后如何获取下载的图像。 I have only the code to download it via URL, here's what I got: 我只有通过URL下载它的代码,这是我得到的:

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
                            }
                        )

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- 据我所知(我只是查阅了作者的Git页面),有以下方法直接访问存储在缓存中的图像 -

You can use the SDImageCache to store an image explicitly to the cache with the following code: 您可以使用SDImageCache使用以下代码将图像显式存储到缓存:

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

Where myImage is the image you want to store and myCacheKey is a unique identifier for the image. 其中myImage是您要存储的图像, myCacheKey是图像的唯一标识符。

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. 这段代码是Objective-C代码,你必须自己“转换”它。

I hope I could help you! 我希望我能帮到你!

From the SDWebImageManager class the downloadImageWithURL: method 来自SDWebImageManager类的downloadImageWithURL:方法

Downloads the image at the given URL if not present in cache or return the cached version otherwise. 如果缓存中不存在,则下载给定URL的图像,否则返回缓存版本。

So if the image is present in cache you are already retrieving it with your code, instead of downloading from the web. 因此,如果图像存在于缓存中,您已经使用代码检索它,而不是从Web下载。

Thanks for answer @beeef but SDWebImage has been updated some part of code: Save image: 感谢您回答@beeef但SDWebImage已经更新了部分代码:保存图片:

 [[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];
    }];

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

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