Kingfisher README

The project's README from github.com/onevcat/Kingfisher. As one markdown file: README.md.

Kingfisher

Kingfisher is a powerful, pure-Swift library for downloading and caching images from the web. It provides you a chance to use a pure-Swift way to work with remote images in your next app.

Features

Kingfisher 101

The simplest use-case is setting an image to an image view with the UIImageView extension:

import Kingfisher

let url = URL(string: "https://example.com/image.png")
imageView.kf.setImage(with: url)

Kingfisher will download the image from url, send it to both memory cache and disk cache, and display it in imageView. When you set it with the same URL later, the image will be retrieved from the cache and shown immediately.

It also works if you use SwiftUI:

var body: some View {
    KFImage(URL(string: "https://example.com/image.png")!)
}

A More Advanced Example

With the powerful options, you can do hard tasks with Kingfisher in a simple way. For example, the code below:

  1. Downloads a high-resolution image.
  2. Downsamples it to match the image view size.
  3. Makes it round cornered with a given radius.
  4. Shows a system indicator and a placeholder image while downloading.
  5. When prepared, it animates the small thumbnail image with a "fade in" effect.
  6. The original large image is also cached to disk for later use, to get rid of downloading it again in a detail view.
  7. A console log is printed when the task finishes, either for success or failure.
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)")
    }
}

It is a common situation I can meet in my daily work. Think about how many lines you need to write without Kingfisher!

Method Chaining

If you are not a fan of the kf extension, you can also prefer to use the KF builder and chained the method invocations. The code below is doing the same thing:

// 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)

And even better, if later you want to switch to SwiftUI, just change the KF above to KFImage, and you've done:

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 }
    }
}

Requirements

Kingfisher 8.0

Kingfisher 7.0

Installation

Refer to one of the following tutorials to install and use the framework:

Alternatively, you can follow either of the methods below.

Swift Package Manager

CocoaPods

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '13.0'
use_frameworks!

target 'MyApp' do
  pod 'Kingfisher', '~> 8.0'
end

Pre-built Framework

  1. Open the release page, download the latest version of Kingfisher from the assets section.
  2. Drag the Kingfisher.xcframework into your project and add it to the target (usually the app target).
  3. Select your target, in the "General" Tab, find the "Frameworks, Libraries, and Embedded Content" section, set the Embed Without Signing to Kingfisher.

Documentation

Check the documentation and tutorials:

Migrating

If you are using an even earlier version, see the guides below to know the steps for migrating.

Other

Future of Kingfisher

I want to keep Kingfisher lightweight. This framework focuses on providing a simple solution for downloading and caching images. This doesn’t mean the framework can’t be improved. Kingfisher is far from perfect, so necessary and useful updates will be made to make it better.

Developments and Tests

Any contributing and pull requests are warmly welcome. However, before you plan to implement some features or try to fix an uncertain issue, it is recommended to open a discussion first. It would be appreciated if your pull requests could build with all tests green. :)

The logo of Kingfisher is inspired by Tangram (七巧板), a dissection puzzle consisting of seven flat shapes from China. I believe she's a kingfisher bird instead of a swift, but someone insists that she is a pigeon. I guess I should give her a name. Hi, guys, do you have any suggestions?

Contact

Follow and contact me on Twitter or Sina Weibo. If you find an issue, open a ticket. Pull requests are warmly welcome as well.

Backers & Sponsors

Open-source projects cannot live long without your help. If you find Kingfisher to be useful, please consider supporting this project by becoming a sponsor. Your user icon or company logo shows up on my blog with a link to your home page.

Become a sponsor through GitHub Sponsors. :heart:

Special thanks to:

imgly

emergetools

License

Kingfisher is released under the MIT license. See LICENSE for details.

Kingfisher (onevcat/Kingfisher)

Searching Database…

这是什么?一个轻量的纯 Swift 库,用于在 iOS、macOS、tvOS、watchOS 和 visionOS 应用中从网络下载并缓存图片——你给它一个 URL,它就会去获取、缓存并显示这张图片。
它是添加到 Xcode 项目里的库,还是一个独立的 app?它是嵌入你自己 app 里的库,不能单独运行。它接入 UIImageView、NSImageView、UIButton 和 SwiftUI 等视图,所以设置一张网络图片只需一行代码。
我怎么把它加到项目里?在 Xcode 里通过 Swift Package Manager 添加(File > Swift Packages > Add Package Dependency,版本选 'Up to Next Major'、8.0.0),或者用 CocoaPods。releases 页面上还有预编译好的 Kingfisher.xcframework,直接拖进你的 target 即可。
它需要哪些系统版本和 Swift 版本?Kingfisher 8.0 需要 iOS 13+、macOS 10.15+、tvOS 13+、watchOS 6+ 或 visionOS 1+,以及 Swift 5.9+。要用 SwiftUI 的话版本要求略高:iOS 14+ / macOS 11+。
安装预编译的 framework 有什么坑?有一个:把 Kingfisher.xcframework 拖进 target 之后,必须在 General 标签页里把它设置为 'Embed Without Signing',否则它不会被正确打包进 app。
它支持 SwiftUI 吗,还是只能用在 UIKit 上?都支持。UIKit 和 AppKit 的视图有 kf.setImage 扩展,SwiftUI 有 KFImage,而且 KF 构建器语法在两者之间几乎不用改动就能通用。
在我的 app 里使用它免费吗?免费——它采用 MIT 许可证发布,可以自由使用,包括商用 app。
第二次加载同一张图片会怎样?会直接从缓存里秒出。Kingfisher 把下载内容存放在多层混合缓存里——内存加磁盘——并自动复用之前下载过的内容,避免重复发起网络请求。
除了下载和显示图片,它还能做别的吗?可以:它内置图片处理器和滤镜(按视图大小降采样、圆角),还有占位图和加载指示器、切换动画、预加载、低数据模式支持,甚至能下载和缓存 Live Photos。
必须整套用吗,还是可以只用缓存那部分?各个组件是独立的——下载器、缓存系统和图片处理器都可以单独使用,还能通过自定义过期时间和大小上限来调整缓存行为。
为什么叫 Kingfisher(翠鸟)?它的 logo 灵感来自七巧板。作者坚持说那只鸟是翠鸟而不是雨燕——不过有些读者咬定那是一只鸽子。
Creator
onevcat
Size
7.9 MB
Stars
24.4K
Age
11y
Commits · 90 Days