Kingfisher 是一个强大的纯 Swift 库,用于从网络下载和缓存图片。它为你提供了一个在你的下一个应用中使用纯 Swift 方式来处理远程图片的机会。
URLSession
的网络或本地提供的数据加载图片。UIImageView
, NSImageView
, NSButton
, UIButton
, NSTextAttachment
, WKInterfaceImage
, TVMonogramView
和 CPListItem
的扩展,可以直接从 URL 设置图片。最简单的用例是使用 UIImageView
扩展将图像设置为图像视图
import Kingfisher
let url = URL(string: "https://example.com/image.png")
imageView.kf.setImage(with: url)
Kingfisher 将从 url
下载图片,将其发送到内存缓存和磁盘缓存,并在 imageView
中显示它。当您稍后使用相同的 URL 设置它时,将从缓存中检索图像并立即显示。
如果您使用 SwiftUI,它也适用
var body: some View {
KFImage(URL(string: "https://example.com/image.png")!)
}
借助强大的选项,您可以使用 Kingfisher 以简单的方式完成艰巨的任务。 例如,下面的代码
let url = URL(string: "https://example.com/high_resolution_image.png")
let processor = DownsamplingImageProcessor(size: imageView.bounds.size)
|> RoundCornerImageProcessor(cornerRadius: 20)
imageView.kf.indicatorType = .activity
imageView.kf.setImage(
with: url,
placeholder: UIImage(named: "placeholderImage"),
options: [
.processor(processor),
.scaleFactor(UIScreen.main.scale),
.transition(.fade(1)),
.cacheOriginalImage
])
{
result in
switch result {
case .success(let value):
print("Task done for: \(value.source.url?.absoluteString ?? "")")
case .failure(let error):
print("Job failed: \(error.localizedDescription)")
}
}
这是我日常工作中经常遇到的情况。想想如果没有 Kingfisher 你需要写多少行代码!
如果您不喜欢 kf
扩展,您也可以选择使用 KF
构建器并链接方法调用。下面的代码执行相同的操作
// Use `kf` extension
imageView.kf.setImage(
with: url,
placeholder: placeholderImage,
options: [
.processor(processor),
.loadDiskFileSynchronously,
.cacheOriginalImage,
.transition(.fade(0.25)),
.lowDataMode(.network(lowResolutionURL))
],
progressBlock: { receivedSize, totalSize in
// Progress updated
},
completionHandler: { result in
// Done
}
)
// Use `KF` builder
KF.url(url)
.placeholder(placeholderImage)
.setProcessor(processor)
.loadDiskFileSynchronously()
.cacheMemoryOnly()
.fade(duration: 0.25)
.lowDataModeSource(.network(lowResolutionURL))
.onProgress { receivedSize, totalSize in }
.onSuccess { result in }
.onFailure { error in }
.set(to: imageView)
更棒的是,如果您以后想切换到 SwiftUI,只需将上面的 KF
更改为 KFImage
,就完成了
struct ContentView: View {
var body: some View {
KFImage.url(url)
.placeholder(placeholderImage)
.setProcessor(processor)
.loadDiskFileSynchronously()
.cacheMemoryOnly()
.fade(duration: 0.25)
.lowDataModeSource(.network(lowResolutionURL))
.onProgress { receivedSize, totalSize in }
.onSuccess { result in }
.onFailure { error in }
}
}
参考以下教程之一来安装和使用该框架
或者,您可以按照以下任何一种方法进行操作。
https://github.com/onevcat/Kingfisher.git
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '13.0'
use_frameworks!
target 'MyApp' do
pod 'Kingfisher', '~> 8.0'
end
Kingfisher.xcframework
拖到您的项目中,并将其添加到目标(通常是应用程序目标)。Embed Without Signing
设置为 Kingfisher。查看文档和教程
如果您使用的是更早的版本,请参阅以下指南,了解迁移步骤。
我想保持 Kingfisher 的轻量级。该框架专注于为下载和缓存图像提供一个简单的解决方案。这并不意味着该框架不能改进。Kingfisher 远非完美,因此将进行必要的和有用的更新以使其更好。
热烈欢迎任何贡献和拉取请求。但是,在您计划实施某些功能或尝试修复不确定的问题之前,建议先进行讨论。如果您的拉取请求可以使用所有测试绿色构建,那就太好了。:)
Kingfisher 的标志灵感来自 Tangram (七巧板),这是一种由七个来自中国的平面形状组成的拼图游戏。我认为她是一只翠鸟而不是 Swift,但有人坚持认为她是一只鸽子。我想我应该给她起个名字。嗨,伙计们,你们有什么建议吗?
在 Twitter 或 新浪微博 上关注并联系我。如果您发现问题,请 打开一个 issue。 也非常欢迎拉取请求。
没有您的帮助,开源项目无法长久存在。 如果您发现 Kingfisher 有用,请考虑成为赞助者来支持这个项目。 您的用户图标或公司徽标会 显示在我的博客上,并链接到您的主页。
通过 GitHub Sponsors 成为赞助者。 ❤️
特别感谢
Kingfisher 在 MIT 许可证下发布。 有关详细信息,请参见 LICENSE。