11. Kingfisher / SDWebImage
目标:下载、缓存、解码与占位图完整可用。
一、安装(SPM)
https://github.com/onevcat/Kingfisher
https://github.com/SDWebImage/SDWebImage
二、Kingfisher 基础用法
import Kingfisher
let url = URL(string: "https://example.com/a.png")
imageView.kf.setImage(with: url)
占位图与过渡
let options: KingfisherOptionsInfo = [
.transition(.fade(0.2)),
.cacheOriginalImage
]
imageView.kf.setImage(
with: url,
placeholder: UIImage(named: "placeholder"),
options: options
)
缓存控制
let cache = ImageCache.default
cache.clearMemoryCache()
cache.clearDiskCache()
三、SDWebImage 基础用法
import SDWebImage
let url = URL(string: "https://example.com/a.png")
imageView.sd_setImage(with: url, placeholderImage: UIImage(named: "placeholder"))
预加载
SDWebImagePrefetcher.shared.prefetchURLs([url])
四、选型建议
- 已有项目长期使用:保持一致
- 需要更灵活的缓存策略:Kingfisher
- 只要稳定下载 + 缓存:任选其一
框架负责下载与缓存,图片尺寸与加载时机仍需开发侧控制。