How to clear all cached images loaded from SDWebImage?
If you want to completely clear the cache do the following:
Obj-c:
- (IBAction)clearCache:(id)sender {
[[SDImageCache sharedImageCache]clearMemory];
[[SDImageCache sharedImageCache]clearDisk];
}
Swift 5
SDImageCache.shared.clearMemory()
SDImageCache.shared.clearDisk()
Swift 3.0
@IBAction func clearCache(sender: UIButton) {
SDImageCache.shared().clearMemory()
SDImageCache.shared().clearDisk()
}
SDWebImage cannot find SDImageCacheType
Okey that was a really trivial problem I don't know why I didn't figure it out earlier. All I need to do is to add one import. Looks like if you use declared blocks you need to add it.
import SDWebImage
Using SDWebImage to copy an image as NSData to Pasteboard
SDWebImage has a cache. You can access it with SDImageCache
. There is customization allowed (different namespaces for caches).
let image = SDImageCache.sharedImageCache.imageFromDiskCacheForKey(url!)
if image != nil
{
let data = UIImagePNGRepresentation(image);
}
You have to check if image
is not nil
because the downloads are asynchrone. So user may trigger collectionView:didSelectItemAtIndexPath:
whereas the image hasn't been downloaded yet, and per se not present in the cache.
Image downloading and caching issue
I think the problem is in your response handler, you are setting cache for url you are requesting, not for url from response, I modified your code a little bit, try, hope it will help you
func downloadImage(url: URL, imageView: UIImageView, placeholder: UIImage? = nil, row: Int) {
imageView.image = placeholder
imageView.cacheUrl = url.absoluteString + "\(row)"
if let cachedImage = imageCache.object(forKey: url.absoluteString as NSString) {
imageView.image = cachedImage
} else {
URLSession.shared.dataTask(with: url) { (data, response, error) in
guard
let response = response as? HTTPURLResponse,
let imageData = data,
let image = UIImage(data: imageData),
let cacheKey = response.url?.absoluteString,
let index = self.arrURLs.firstIndex(of: cacheKey)
else { return }
DispatchQueue.main.async {
if cacheKey + "\(index)" != imageView.cacheUrl { return }
imageView.image = image
self.imageCache.setObject(image, forKey: cacheKey as NSString)
}
}.resume()
}
}
And
var associateObjectValue: Int = 0
extension UIImageView {
fileprivate var cacheUrl: String? {
get {
return objc_getAssociatedObject(self, &associateObjectValue) as? String
}
set {
return objc_setAssociatedObject(self, &associateObjectValue, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN)
}
}
}
UPDATED:
Related Topics
How to Convert String to Date Without Time in Swift 3
Invalid Update: Invalid Number of Rows in Section 1
Do Protocols Have an Effect on The Retain Count
Switch Statement Where Value Is Int But Case Can Contain Array
Swiftui Change on Multilevel Children Published Object Change
Public Default Init in Protocol
Swiftui - How to Change The Button's Image on Click
Swift "Print" Doesn't Appear in Stdout But 3Rd Party C Library Logs Do When Running in Docker on Ecs
Swift How to Make List Start at The Top with No Padding
Crashlytics Doesn't Work with Xcode 10 Beta
How to Dynamically Hide Navigation Back Button in Swiftui
Using Nsdate to Get Date for Easter
Add a File Generated by Run Script into The Test Target Compilation List in Xcode
Xcode 6.1 Swift Extensions - Sourcekit Service Crash
Swift: Guard Let and Where - The Priority
Http Status 415 When Using Alamofire Multipart Upload
Swift Package: Ld: Warning: Dylib Was Built for Newer Macos Version (11.0) Than Being Linked (10.15)