Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom DataLoader without native URL cache #781

Open
alexacz opened this issue May 9, 2024 · 1 comment
Open

Custom DataLoader without native URL cache #781

alexacz opened this issue May 9, 2024 · 1 comment
Labels

Comments

@alexacz
Copy link

alexacz commented May 9, 2024

I'm trying to implement a custom plugin.
I download SVG data from URL, then I generate png image from SVG.
I don't want to cache SVG data (URL cache), I would cache png image, I would use LRU disk cache only for that.
My question is:
How to implement custom DataLoader without native URL cache but with LRU disk cache?

Below is my current implementation. The issue: data is caching to memory only even if I have configured the LRU cache in my custom ImagePipeline.

public final class SvgDataLoader: Nuke.DataLoading {
  ...
  public func loadData(with request: URLRequest,
    didReceiveData: @escaping (Data, URLResponse) -> Void,
    completion: @escaping (Error?) -> Void) -> Nuke.Cancellable {
      // download svg, convert to png
      ...
      didReceiveData(pngData, URLResponse(url: request.url!,
        mimeType: "image/png",
        expectedContentLength: pngData.count,
        textEncodingName: "utf8"))
      ...
 }
}
let customPipeline = ImagePipeline {
    let dataCache = try? DataCache(name: "com.qazwsxedcrfv.datacache")
    dataCache?.sizeLimit =  100 * 1024 * 1024    // 100 MiB
    dataCache?.sweepInterval = 3600 * 24 * 180   // 180 days
    $0.dataCache = dataCache
    $0.dataLoader = SvgDataLoader()
}
...
LazyImage(request:request) { state in
    if let image = state.image {
        image.resizable().scaledToFit().padding(10)
    } else if state.error != nil {
        Color(.systemBackground)
            .frame(minWidth: 140, minHeight: 100)
            .overlay{
                Image(systemName: "exclamationmark.octagon").font(.body)
            }
    } else {
        Color(.systemBackground)
            .frame(minWidth: 140, minHeight: 100)
            .overlay(ProgressView())
    }
}
.pipeline(customPipeline)
...
@kean kean added the question label May 18, 2024
@kean
Copy link
Owner

kean commented May 18, 2024

Hey,

I would suggest trying DataCachePolicy.storeEncodedImages. It will encode the processed images (.png) and stored them in the disk cache (aka LRU cache, DataCache). It seems like exactly what you are looking for.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants