Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

清除图片的缓存 #2204

Open
iCeors opened this issue Feb 2, 2024 · 3 comments
Open

清除图片的缓存 #2204

iCeors opened this issue Feb 2, 2024 · 3 comments

Comments

@iCeors
Copy link

iCeors commented Feb 2, 2024

Check List

Thanks for considering to open an issue. Before you submit your issue, please confirm these boxes are checked.

Issue Description

缓存清不掉

What

同一张《本地》(本地沙盒里,本地生成的)图片,多个imageview去展示,因为这些iv的大小不一样,结合downsample之后,他们缓存key也不一样。
用户可以修改本地图片,变化后,我需要清理掉所有【这张图片】的缓存,以便让用户看到最新的样子。
我看了源码,自己构建了相对应的缓存key(把所有可能出现的imageview的大小记录下来,无脑添加到后缀,构建缓存key,无脑调用mem和disk的清理方法),去清理,还是在某些场景下,莫名其妙的清不掉,非常影响用户体验。
只想要一个api可以清理掉【这张图片】的缓存,也就是说,不论是经过什么变换,缓存里有多少个不同变换的key,清理【这张图片】的所有缓存。

Reproduce

[The steps to reproduce this issue. What is the url you were trying to load, where did you put your code, etc.]

Other Comment

[Add anything else here]

@iCeors
Copy link
Author

iCeors commented Feb 2, 2024

复盘发现,是因为app重启,丢失了所有processor的内容,导致缓存清理不掉。
把processor融合到图片缓存key,而不提供单纯的清理某张图片派生出所有图片缓存的api,面对一些常见场景的时候,有些无力。。。

@onevcat
Copy link
Owner

onevcat commented Feb 4, 2024

这个场景,我个人现在的建议是,你可以为每个URL设置一个 cache (伪代码,不一定正确):

var caches = [String: ImageCache]()

func imageCacheForURL(_ url: URL) -> ImageCache {
    caches[url.absoluteString] ?? {
        let c = ImageCache(name: url.absoluteString)
        caches[url.absoluteString] = c
        return c
    }()
}

let imageView: UIImageView
imageView.kf.setImage(with: url, options: [.processor(processor1), .targetCache(imageCacheForURL(url))])
imageView.kf.setImage(with: url, options: [.processor(processor2), .targetCache(imageCacheForURL(url))])

// Later
imageCacheForURL(url).clearCache()

@iCeors
Copy link
Author

iCeors commented Feb 4, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants