KingfisherWebP

repository·main·Indexed 43 days ago

https://github.com/yeatse/kingfisherwebp

An extension for the Kingfisher image loading library that adds support for the WebP format, including animated WebP images, via custom ImageProcessors and CacheSerializers. Requires iOS 12 or above.

Tokens
707
Snippets
4
Records
4
Agent score
6%

What's inside KingfisherWebP

  1. Configure the 'Accept' header for WebP support

    main

    Some image servers require the Accept HTTP header to include image/webp to serve WebP content. You can achieve this by creating an AnyModifier and adding it to the KingfisherManager.shared.defaultOptions.

    let modifier = AnyModifier { request in
        var req = request
        req.addValue("image/webp */*", forHTTPHeaderField: "Accept")
        return req
    }
    
    KingfisherManager.shared.defaultOptions += [
        .requestModifier(modifier),
        // ... other options
    ]
  2. Set WebP as the global default for Kingfisher

    main

    To avoid passing options to every single image request, you can set WebPProcessor.default and WebPSerializer.default as global defaults on the KingfisherManager. This should be done once during application launch.

    // somewhere after your application launches...
    
    KingfisherManager.shared.defaultOptions += [
      .processor(WebPProcessor.default),
      .cacheSerializer(WebPSerializer.default)
    ]
    
    // You can now use webp in Kingfisher like any other format
    imageView.kf.setImage(with: url)
  3. Install KingfisherWebP via CocoaPods, SPM, or Carthage

    main

    KingfisherWebP can be integrated into your project using several dependency managers. Ensure your project meets the requirement of iOS 12 or above.

    CocoaPods

    Add this line to your Podfile:

    pod "KingfisherWebP"

    Swift Package Manager

    In Xcode, add the following package repository URL: https://github.com/yeatse/KingfisherWebP.git

    Carthage

    Add the following to your Cartfile. Note: Because KingfisherWebP is built on libwebp, you must also include the libwebp dependency:

    github "yeatse/KingfisherWebP" ~> 1.4.0
    github "onevcat/Kingfisher" ~> 7.0.0
    github "SDWebImage/libwebp-Xcode" ~> 1.1.0
  4. Use KingfisherWebP to display WebP images

    main

    KingfisherWebP extends the Kingfisher library by providing a WebPProcessor and a WebPSerializer. To display WebP images (including animated WebP) from a network URL, pass these to your KingfisherOptionsInfo.

    let url = URL(string: "url_of_your_webp_image")
    imageView.kf.setImage(with: url, options: [.processor(WebPProcessor.default), .cacheSerializer(WebPSerializer.default)])