Hello I am using the SDWebImage framework in a project and I want to download and cache images, but I think my code is storing an image in the cache twice? Is there any way
Swift 4:
SDWebImageManager.shared().loadImage(
with: URL(string: imageUrl),
options: .highPriority,
progress: nil) { (image, data, error, cacheType, isFinished, imageUrl) in
print(isFinished)
}
SDWebImageManager *manager = [SDWebImageManager sharedManager];
[manager downloadImageWithURL:ImageUrl options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize)
{
} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
if(image){
NSLog(@"image=====%@",image);
}
}];
Yes is possible to download the image using SDWebImage
And store into local memory Manually.
Downloaded Image store into local memory using SDWebImage
func saveImage(url: URL, toCache: UIImage?, complation: @escaping SDWebImageNoParamsBlock) {
guard let toCache = toCache else { return }
let manager = SDWebImageManager.shared()
if let key = manager.cacheKey(for: url) {
manager.imageCache?.store(toCache, forKey: key, completion: complation)
}
}
Load image from memory using image URL
static func imageFromMemory(for url: String) -> UIImage? {
if let encoded = url.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed),
let url = URL(string: encoded) {
let manager = SDWebImageManager.shared()
if let key: String = manager.cacheKey(for: url),
let image = manager.imageCache?.imageFromMemoryCache(forKey: key) {
return image
}
}
return nil
}
SDWebImage caches the image both to disk as well as memory. Let's go through this:
So, you don't need to worry about caching. SDWebImage takes care of it pretty damn well.
You can do custom implementation for caching as well as image refresh from the cache in case you want the settings as per your HTTP caching header as well.
You can find the complete details on their github page here.
In Swift use the code below to download an image and to store it in the cache:
//SDWebImageManager download image with High Priority
SDWebImageManager.sharedManager().downloadImageWithURL(NSURL(string: imageUrl), options: SDWebImageOptions.HighPriority, progress: { (receivedSize :Int, ExpectedSize :Int) in
SVProgressHUD.show()
}, completed: { (image :UIImage!, error:NSError!, cacheType :SDImageCacheType, finished :Bool,imageUrl: NSURL!) in
if(finished) {
SVProgressHUD.dismiss()
if((image) != nil) {
//image downloaded do your stuff
}
}
})
Swift 3 version:
SDWebImageManager.shared().downloadImage(with: NSURL(string: "...") as URL!, options: .continueInBackground, progress: {
(receivedSize :Int, ExpectedSize :Int) in
}, completed: {
(image : UIImage?, error : Error?, cacheType : SDImageCacheType, finished : Bool, url : URL?) in
})
Objective-C version:
[[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:imageUrl] 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:CUSTOM_KEY toDisk:YES];
}
}];