Kingfisher is a powerful pure Swift library used for downloading and caching images from the web

Kingfisher is a powerful pure Swift library used for downloading and caching images from the web

2022-12-12 0 1,938
Resource Number 49634 Last Updated 2025-02-21
¥ 0HKD Upgrade VIP
Download Now Matters needing attention
Can't download? Please contact customer service to submit a link error!
Value-added Service: Installation Guide Environment Configuration Secondary Development Template Modification Source Code Installation

The Kingfisher recommended in this issue is a powerful pure Swift library used for downloading and caching images from the web.

Kingfisher is a powerful pure Swift library used for downloading and caching images from the web插图

Kingfisher gives you the opportunity to use pure Swift to process remote images in your next application.

Kingfisher features

  • Asynchronous image download and caching.
  • Load images from URLSession based on network or locally provided data.
  • Provide useful image processors and filters.
  • Multi layer hybrid cache of memory and disk.
  • Fine control over caching behavior. Customizable expiration dates and size restrictions.
  • Can cancel downloads and automatically reuse previously downloaded content to improve performance.
  • Independent components. Use downloaders, caching systems, and image processors as needed.
  • Pre fetch images and display them from cache to enhance your application.
  • UIImageView, NSImageView, NSButton, UIButton, NSTextAttachment,The extension WKInterface Image is used to directly set images from URLs.TVMonogramViewCPListItem
  • Built in transition animation when setting up images.
  • Customizable placeholders and indicators when loading images.
  • Easy to expand image processing and image formats.
  • Low data mode support.
  • SwiftUI support.

Kingfisher

import Kingfisher

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

Kingfisher will download the image URL, send it to the memory cache and disk cache, and use imageView When you set it up with the same URL later, the image will be retrieved from the cache and displayed immediately.

If you use SwiftUI, it can also work:

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

A more advanced example

With powerful options, you can complete challenging tasks in a simple way with Kingfisher. For example, the following code:

  • Download high-resolution images.
  • Downsample it to match the size of the image view.
  • Make it rotate at a given radius.
  • Display system indicators and placeholder images during download.
  • After preparation, it will animate the small thumbnail image with a “fade in” effect.
  • The original large image is also cached to disk for future use to avoid downloading it again in the detailed view.
  • When the task is completed, the console log will be printed, whether it is successful or failed.
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)")
    }
}

method chaining

If you are not a fan of KF extensions, you may also prefer to use KF builders and link method calls. The following code does 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)

Even better, if you want to switch to SwiftUI in the future, just change the content on KF to KFImage, and you’ll be 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 }
    }
}

require

  • iOS 12.0+ / macOS 10.14+ / tvOS 12.0+ / watchOS 5.0+(If you only use UIKit/AppKit)
  • iOS 14.0+ / macOS 11.0+ / tvOS 14.0+ / watchOS 7.0+(If you use it in SwiftUI)
  • Swift 5.0+

installation guide

Swift Package Manager

  • Select File>Swift Package>Add Package Dependency. https://github.com/onevcat/Kingfisher.git Enter in the ‘Select Package Repository’ dialog box.
  • On the next page, specify the version parsing rule as’ Up to Next Major ‘, with the earliest version being’ 7.0.0 ‘.
  • After checking the source code and parsing the version in Xcode, you can select the “Kingfisher” library and add it to your application target.

Kingfisher is a powerful pure Swift library used for downloading and caching images from the web插图1

Cocoa

CocoaPods is the dependency manager for the Cocoa project. You can install it using the following command:

$ gem install cocoapods

To integrate Kingfisher into your Xcode project using CocoaPods, specify it as your Podfile:

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

target 'MyApp' do
  # your other pod
  # ...
  pod 'Kingfisher', '~> 7.0'
end

Then, run the following command:

$ pod install

After installing any content from CocoaPods, you should open {Project}. xcworkspace instead of opening it. {Project}.xcodeproj

Carthage

Carthage is a decentralized dependency manager for Cocoa applications. To install the Carthage tool, you can use Homebrew.

$ brew update
$ brew install carthage

To integrate Kingfisher into your Xcode project using Carthage, please go to your Cartfile:

github "onevcat/Kingfisher" ~> 7.0

Then, run the following command to build the Kingfisher framework:

$ carthage update Kingfisher --platform iOS
# Or `--platform macOS`, `--platform tvOS`, `--platform watchOS`

Finally, you need to manually set up your Xcode project to add the Kingfisher framework:

  • On the “General” settings tab of the application target, in the “Linking Frameworks and Libraries” section, drag and drop each framework you want to use from the Carthage/Build folder on the disk.
  • On the “Build Phases” settings tab of the application target, click the “+” icon and select “New Run Script Phase”. Create a running script that includes the following content:
/usr/local/bin/carthage copy-frameworks
  1. Add the path of the framework to be used under ‘Input Files’:
$(SRCROOT)/Carthage/Build/iOS/Kingfisher.framework
  1. Add the path of the copied framework to the ‘Output File’:
$(BUILT_PRODUCTS_DIR)/$(FRAMEWORKS_FOLDER_PATH)/Kingfisher.framework

—END—

Open source protocol:MIT license

资源下载此资源为免费资源立即下载
Telegram:@John_Software

Disclaimer: This article is published by a third party and represents the views of the author only and has nothing to do with this website. This site does not make any guarantee or commitment to the authenticity, completeness and timeliness of this article and all or part of its content, please readers for reference only, and please verify the relevant content. The publication or republication of articles by this website for the purpose of conveying more information does not mean that it endorses its views or confirms its description, nor does it mean that this website is responsible for its authenticity.

Ictcoder Free Source Code Kingfisher is a powerful pure Swift library used for downloading and caching images from the web https://ictcoder.com/kingfisher-is-a-powerful-pure-swift-library-used-for-downloading-and-caching-from-web/

Qizhuwang Source Code Trading Platform

Q&A
  • 1. Automatic: After making an online payment, click the (Download) link to download the source code; 2. Manual: Contact the seller or the official to check if the template is consistent. Then, place an order and make payment online. The seller ships the goods, and both parties inspect and confirm that there are no issues. ICTcoder will then settle the payment for the seller. Note: Please ensure to place your order and make payment through ICTcoder. If you do not place your order and make payment through ICTcoder, and the seller sends fake source code or encounters any issues, ICTcoder will not assist in resolving them, nor can we guarantee your funds!
View details
  • 1. Default transaction cycle for source code: The seller manually ships the goods within 1-3 days. The amount paid by the user will be held in escrow by ICTcoder until 7 days after the transaction is completed and both parties confirm that there are no issues. ICTcoder will then settle with the seller. In case of any disputes, ICTcoder will have staff to assist in handling until the dispute is resolved or a refund is made! If the buyer places an order and makes payment not through ICTcoder, any issues and disputes have nothing to do with ICTcoder, and ICTcoder will not be responsible for any liabilities!
View details
  • 1. ICTcoder will permanently archive the transaction process between both parties and snapshots of the traded goods to ensure the authenticity, validity, and security of the transaction! 2. ICTcoder cannot guarantee services such as "permanent package updates" and "permanent technical support" after the merchant's commitment. Buyers are advised to identify these services on their own. If necessary, they can contact ICTcoder for assistance; 3. When both website demonstration and image demonstration exist in the source code, and the text descriptions of the website and images are inconsistent, the text description of the image shall prevail as the basis for dispute resolution (excluding special statements or agreements); 4. If there is no statement such as "no legal basis for refund" or similar content, any indication on the product that "once sold, no refunds will be supported" or other similar declarations shall be deemed invalid; 5. Before the buyer places an order and makes payment, the transaction details agreed upon by both parties via WhatsApp or email can also serve as the basis for dispute resolution (in case of any inconsistency between the agreement and the description of the conflict, the agreement shall prevail); 6. Since chat records and email records can serve as the basis for dispute resolution, both parties should only communicate with each other through the contact information left on the system when contacting each other, in order to prevent the other party from denying their own commitments. 7. Although the probability of disputes is low, it is essential to retain important information such as chat records, text messages, and email records, in case a dispute arises, so that ICTcoder can intervene quickly.
View details
  • 1. As a third-party intermediary platform, ICTcoder solely protects transaction security and the rights and interests of both buyers and sellers based on the transaction contract (product description, agreed content before the transaction); 2. For online trading projects not on the ICTcoder platform, any consequences are unrelated to this platform; regardless of the reason why the seller requests an offline transaction, please contact the administrator to report.
View details

Related Source code

ICTcoder Customer Service

24-hour online professional services