I am using af_setImage() to set an image to a UIImageView. I need to cache image and if it's cached, I want it not to make transition. I have tried some codes below but the image cache with identifier always returns nil. It seems that imageCache.add() is called every time but it doesn't add the cache.
let imageCache = AutoPurgingImageCache()
if let image = imageCache.image(withIdentifier: post.mainImage) {
cell.postImageView.image = image
} else {
cell.postImageView.af_setImage(
withURL: URL(string: post.mainImage)!,
placeholderImage: PostCellView.defaultImage,
imageTransition: .crossDissolve(0.5),
completion: { response in
if let image = response.result.value, response.result.isSuccess {
imageCache.add(image, withIdentifier: post.mainImage)
}
}
)
}
What am I wrong?
Thank you in advance( ´ ▽ ` )
I found that the main issue with AutoPurgingImageCache is that the new instances do not inherit cache from the previous objects, so if you put this code inside the viewDidLoad function the cache its always return nil
So I manage to solve with a static object, this is the code
Be aware that the af_setImage function already use cache, so you must use the cache object for other purpose than display a image inside a UIImage object, for example display a image inside a textView, with a NSTextAttachment