Third-Party Libraries

How to integrate third-party libraries

The image pipeline is easy to customize. This guide contains some examples of how you can extend it.

Networking Libraries #

By default, Nuke uses a Foundation.URLSession for all the networking. Apps that have a custom networking layer can use it instead.

Nuke has an Alamofire plugin that allows you to load image data using Alamofire.SessionManager. If you want to use Nuke with Alamofire, follow the plugin’s docs.

If you’d like to use some other networking library or use your custom code, all you need to do is implement the DataLoading protocol consisting of a single method. You can use Alamofire plugin as a starting point.

/// Loads data.
public protocol DataLoading {
    /// - parameter didReceiveData: Can be called multiple times if streaming
    /// is supported.
    /// - parameter completion: Must be called once after all (or none in case
    /// of an error) `didReceiveData` closures have been called.
    func loadData(with request: URLRequest,
                  didReceiveData: @escaping (Data, URLResponse) -> Void,
                  completion: @escaping (Error?) -> Void) -> Cancellable
}

Caching Libraries #

By default, Nuke uses a Foundation.URLCache, a part of the Foundation URL Loading System. But sometimes, the built-in cache can not be performant enough or might not fit your needs.

See Image Caching to learn more about HTTP cache. To learn more about caching in Nuke and how to configure it, see Caching.

Nuke can be configured to use any third-party caching library with two simple steps.

Step 1. Add conformance to DataCaching protocol:

extension DFCache: DataCaching {
    public func cachedData(for key: String) -> Data? {
        return self.cachedData(forKey: key)
    }

    public func storeData(_ data: Data, for key: String) {
        self.store(data, forKey: key)
    }
}

Step 2. Configure ImagePipeline to use the new cache.

ImagePipeline.shared = ImagePipeline {
    let conf = URLSessionConfiguration.default
    conf.urlCache = nil // Disable native URLCache
    $0.dataLoader = DataLoader(configuration: conf)

    $0.dataCache = DFCache(name: "com.github.kean.Nuke.DFCache", memoryCache: nil)
}

Starting with Nuke 7, it ships with an aggressive disk cache built-in. See DataCache for more info.

Vector Images Libraries #

To render SVG, consider using SwiftSVG, SVG, or other frameworks. Here is an example of SwiftSVG being used to render vector images:

ImageDecoderRegistry.shared.register { context in
    // Replace this with whatever works for you. There are no magic numbers
    // for SVG like are used for other binary formats, it's just XML.
    let isSVG = context.urlResponse?.url?.absoluteString.hasSuffix(".svg") ?? false
    return isSVG ? ImageDecoders.Empty() : nil
}

let url = URL(string: "https://upload.wikimedia.org/wikipedia/commons/9/9d/Swift_logo.svg")!
ImagePipeline.shared.loadImage(with: url) { [weak self] result in
    guard let self = self, let data = try? result.get().container.data else {
        return
    }
    // You can render image using whatever size you want, vector!
    let targetBounds = CGRect(origin: .zero, size: CGSize(width: 300, height: 300))
    let svgView = UIView(SVGData: data) { layer in
        layer.fillColor = UIColor.orange.cgColor
        layer.resizeToFit(targetBounds)
    }
    self.view.addSubview(svgView)
    svgView.bounds = targetBounds
    svgView.center = self.view.center
}