Sdwebimage, Swift: Sdwebimagerefreshcached Unresolved Identifier

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



Leave a reply



Submit