Get SDWebImage Cache Image

安稳与你 提交于 2019-12-06 12:21:56

问题


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

回答1:


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.




回答2:


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!




回答3:


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


来源:https://stackoverflow.com/questions/37007553/get-sdwebimage-cache-image

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