SDWebImage refreshing - ios

I am using the SDWebImage to load the image from URL. When image is updated on the server, then image is not updating in the image view even i have used options: .refreshCached. After updating image, URL remains same.
Can i directly change image in cache for this URL?
I googled for this question,i got that i can not change it in cache directly. If i can not change image in cache directly for specify URL, Is it possible to clear cached value for specific key (URL)?
self.imgView.sd_setImage(with: url, placeholderImage: #imageLiteral(resourceName: "Timg"), options: .refreshCached, completed: { (image: UIImage?, error: Error?, cacheType: SDImageCacheType, imageURL: URL?) in
if image != nil{
self.imgView.contentMode = .scaleAspectFill
}
})

Looks like the issue has already been raised in the official GitHub page of SDWebImage. Clearly the answer suggests that [[SDImageCache sharedImageCache] removeImageForKey:image_url fromDisk:YES]; is not doing fair enough job to clear the image.
Suggested answer says
[[SDImageCache sharedImageCache] clearMemory];
[[SDImageCache sharedImageCache] cleanDisk];
Now whether you need both the statements or not depends on how you have configured SDImageCache.
What do they mean ?
SDWebImage caches images extensively, it keeps the copy of cache in RAM as well as in HardDisk. When an image is requested, it hits the RAM Cache first to find the image, if not found then hits disk cache. And loads RAM cache from disk cache.
So clearing simply from disk will still leave image in Disk cache and when requested an image rather than downloading from web it rather loads from Disk and keeps a copy in RAM for future use. So it defeats the idea of deleting the image from cache.
I know its not the answer you are specifically looking for, but unfortunately am not aware of deleting specific image from SDImageCache.

[[SDImageCache sharedImageCache] removeImageForKey:image_url fromDisk:YES];

Related

How to set image using Kingfisher when image url did not change but image changed

How to retrieve correct image using Kingfisher when image url did not change but image changed such as Twitter or GitHub.
SDWebImage has a option [SDWebImageRefreshCached] to solve the problem.
/**
* Even if the image is cached, respect the HTTP response cache control, and refresh the image from remote location if needed.
* The disk caching will be handled by NSURLCache instead of SDWebImage leading to slight performance degradation.
* This option helps deal with images changing behind the same request URL, e.g. Facebook graph api profile pics.
* If a cached image is refreshed, the completion block is called once with the cached image and again with the final image.
*
* Use this flag only if you can't make your URLs static with embedded cache busting parameter.
*/
SDWebImageRefreshCached = 1 << 4,
How to solve the problem in Kingfisher?
Kingfisher has forceRefresh option to force downloading image again skiping cache
imageView.kf.setImage(with: url, options: [.forceRefresh])
refer this github thread for more details

SDWebImage caching is taking time to load the image from cache in uicollectionview cell

I am using SDWebImage library to load images in a collection view .
Library downloads the image from server and caches but when I quit my app and re-run , it takes time to load image from cache.
( placeholder image is displayed for 2-3 secs sometimes even more )
Code to load image using SDWebImage:
cell.imgPost.sd_setImage(with: URL(string: (post.lowQualityImgUrl) ?? ""), placeholderImage: UIImage(named:"greybg") , options: .refreshCached, progress: { (recievedSize, expectedSize) in
progressIndicatorView.progress = CGFloat(recievedSize)/CGFloat(expectedSize)
}, completed: { (image, error, cachetype, url) in
if image != nil{
DispatchQueue.main.async {
progresView.removeFromSuperview()
progressIndicatorView.removeFromSuperview()
cell.progressIndicatorView?.isHidden = true
cell.imgPost.image = image
}
}
})
Here the caching option used is refresh cache.
Question:
I don't know why collection view cells can't load the images instantly using sdwebimage . As it says sdwebimage is best library for image caching .
Can anyone point out solution /problem ?
EDIT
I did left comment in their library , I think it's never a good library.
https://github.com/rs/SDWebImage/issues/138
Thanks in advance.
From SDWebImage documentation, for refreshCached
* Even if the image is cached, respect the HTTP response cache control, and refresh the image from remote location if needed.
* The disk caching will be handled by NSURLCache instead of SDWebImage leading to slight performance degradation.
* This option helps deal with images changing behind the same request URL, e.g. Facebook graph api profile pics.
* If a cached image is refreshed, the completion block is called once with the cached image and again with the final image.
*
* Use this flag only if you can't make your URLs static with embedded cache busting parameter.
You should not use refreshCached if you don't want images to be cached unless you get different images in same URL. Leave it to default set up and it will handle everything for you.
EDIT: Just leave options as empty array, []. It should continue with default setup. Your delay is probably because you still have refreshedCache in your code.
cell.imgPost.sd_setImage(with: NSURL() as URL!, placeholderImage: UIImage(), options: [], progress: { (_, _) in
}, completed: { (_, _, _, _) in
})

Alamofire Image: Fetch Image from AutoPurgingImageCache after af_setImageWithURL()

I use AlamofireImage in my project. I use someImageView.af_setImageWithURL(url) a lot.
However at some point I need to fetch an image manually from the imageCache, because I do not want to apply it to an UIImageView.
This brings up 2 problems:
I can access the image cache directly by creating one with let imageCache = AutoPurgingImageCache(). Is this the same cache as .af_setImageWithURL() uses (Singleton)? Or is this a new one? Which is worthless in my case, because I could not profit from the prefilled cache.
Given this is the same cache: How can I fetch the correct image? I thought about imageCache.imageForRequest(NSUrlRequest). Will this match on the images I downloaded before with .af_setImageWithURL() if the NSUrlRequest does use the same NSURL?
Question 1
That is not the same cache that UIImageView uses. If you need access the same cache, you can do so by UIImageView.af_sharedImageDownloader.imageCache.
Question 2
You will need to use the exact same NSURLRequest and the same ImageFilter. If you use the same NSURLRequest and the same ImageFilter, the image will be directly fetched from the cache if it exists. If you are using a different ImageFilter, then the original image will most likely be pulled from the NSURLCache, then the ImageFilter will be run over it, placed into the AutoPurgingImageCache and returned. If the NSURLRequest is different, the new image will need to be downloaded again.
This is answer to the comment: How to download multiple images, save them to cache and get from cache.
Following code will download images from array of URLs and cache them in-memory.
let imageDownloader = ImageDownloader()
var URLRequests = [URLRequestConvertible]()
URLRequests.append(NSURLRequest(URL: NSURL(string: "https://some/image/some.png")!))
As #cnoon mentioned, to set an image use myUIImageView.af_setImageWithURL(imageUrl)
This will load image from cache if present. If it doesn't exist, then network call will happen

Get unique Image file path in application Directory (Cache) w.r.t unique URL of images stored at server in SDWEBIMAGE

I am using SDWEBIMAGE library for show images in ImageView in my app.
I am stuck in find unique file path of stored image in cache by SDWEBIMAGE. I need a file path of particular Image w.r.t it's URL in cached images in Document Directory of app.
So, imagepath must be unique for every URL.
I was go through this This Discussion link But no luck.
Any help will be appreciated.
From the documentation:
To lookup the cache, you use the queryDiskCacheForKey:done: method. If
the method returns nil, it means the cache doesn't currently own the
image. You are thus responsible for generating and caching it. The
cache key is an application unique identifier for the image to cache.
It is generally the absolute URL of the image.
SDImageCache *imageCache = [[SDImageCache alloc] initWithNamespace:#"myNamespace"];
[imageCache queryDiskCacheForKey:myCacheKey done:^(UIImage *image)
{
// image is not nil if image was found
}];
UPDATE
You can get the path to your image like this:
NSString *myImgKey;//this is the key for the image you want the path for
[[SDImageCache sharedImageCache] defaultCachePathForKey:myImgKey];
in case you do not use the default location, you can use:
- (NSString *)cachePathForKey:(NSString *)key inPath:(NSString *)path;
This function returns the path for a certain key, but you will have to pass it your cache's root folder.
SDwebImage always encrypt your image name. But if you want to access the image you can use your URL as the key to read the image from SDWebImage (- (UIImage *)imageFromDiskCacheForKey:(NSString *)key;). See the SDWebImageCache.
https://github.com/rs/SDWebImage/blob/master/SDWebImage/SDImageCache.h
Still if you want to access the path you can create the category of SDWebImageCache and use private method
(NSString *)defaultCachePathForKey:(NSString *)key
See - How to get filesystem path to image cached with SDWebImage (iOS)

iOS Caching images with AFImageCache doesn't seem to work

I'm trying to cache some images loaded from a URL by using AFImageCache, without success... I'm not sure If i'm doing anything wrong and would love your opinions if you've ever used this library.
I'm getting an image from a NSURLRequest, and then caching it as follows
UIImage *img; //This image is a png icon returned from the server
NSData *imgData = UIImagePNGRepresentation(img); // Always checking length to make sure its more than 0, and it is
[[AFImageCache sharedImageCache] cacheImageData: imgData
forURL: myURL
cacheName: #"IconCache"];
On the next run, i'll try to check if the image was cached, but will always get nil for some reason.
UIImage *myIcon = [[AFImageCache sharedImageCache] cachedImageForURL: myURL cacheName: #"IconCache"];
NSLog("%#", myIcon); // output (null)
Any ideas or thoughts about this?
Thanks.
What version of AFNetworking are you using? AFImageCache was removed from the public API a long time ago. If you want to do image cacheing, use NSURLCache, which caches all network responses on a system level. AFImageCache has always just been a temporary cache used to improve the performance of image loading in scroll views.

Resources