Skip to content

Prefetching with URL then loading in SwiftUI by custom key #2051

Closed Answered by onevcat
davidkessler-ch asked this question in Q&A
Discussion options

You must be logged in to vote

Hi,

I guess this should do the trick. Instead of using the URL-based loading, try to define an ImageDataProvider:

struct CacheProvider: ImageDataProvider {

    enum E: Error {
        case malformedData
        case notInCache
    }

    let cacheKey: String
    let options: KingfisherOptionsInfo?

    func data(handler: @escaping (Result<Data, Error>) -> Void) {
        ImageCache.default.retrieveImage(forKey: cacheKey, options: .init(options)) { result in
            switch result {
            case .success(let r):
                if let image = r.image {
                    if let data = image.pngData() {
                        handler(.success(data))
                    } else {

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@onevcat
Comment options

Comment options

You must be logged in to vote
1 reply
@davidkessler-ch
Comment options

Answer selected by davidkessler-ch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants