孙悟空
孙悟空是一个纯Swift库,用于从Firebase存储和URL下载和缓存图片。该项目受到流行的FirebaseStorageCache和MapleBacon的极大启发。
要求
Swift 4
iOS 9.0+
Xcode 9+
该项目假设您已经为iOS配置了Firebase。
安装
孙悟空可以通过CocoaPods使用。要安装它,只需将以下行添加到Podfile中
pod 'SunWuKong'
用法
UIImageView
- 使用存储引用
let ref: StorageReference = ....
imageView.wk_setImage(with: ref)
- 带有占位符、进度指示器和完成处理程序
func setImageWithStorageReference() {
let ref: StorageReference = ....
imageView.wk_setImage(with: ref, placeholder: UIImage(named: "placeholder"), progress: { received, total in
// Report progress
}, completion: { [weak self] image in
// Do something else with the image
})
}
- 使用URL
let url: URL = ....
imageView.wk_setImage(with: url)
- 带有占位符、进度指示器和完成处理程序
func setImageWithURL() {
let url: URL = ....
imageView.wk_setImage(with: url, placeholder: UIImage(named: "placeholder"), progress: { received, total in
// Report progress
}, completion: { [weak self] image in
// Do something else with the image
})
}
使用默认共享缓存
let ref: StorageReference = ...
SunWuKong.shared.image(with: ref) { [weak self] image in
// do something with your image
}
创建自定义存储缓存
let oneDaySeconds: TimeInterval = 60 * 60 * 24
let oneDayCache = DiskCache(name: "customCache", cacheDuration: oneDaySeconds)
let wukongCache = SunWuKong(cache: oneDayCache)
清理/修剪缓存
在您的AppDelegate的didFinishLaunchingWithOptions
中,应调用您的磁盘缓存的prune()
方法以删除任何旧文件
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
SunWuKong.shared.prune()
return true
}
作者
版权
SunWuKong可在MIT许可下使用。有关更多信息,请参阅LICENSE文件。