Unable to set a custom cache for KFImage in SwiftUI

275 Views Asked by At

I want to load an image from a custom cache (named "offline") for KFImage when using it in a SwiftUI view. I have tried various approaches, but I am still unable to apply the custom cache to KFImage. It works fine when setting images in UIKit.

Note: I have two caches

Default - General purpose. Memory and Disk expire after a couple of hours. Offline - for images that need to persist on disk forever or until internet access is regained an images are successfully uploaded to DB.

let url = URL(string: item.photoURL ?? "") ?? URL(string: "")!
let resource: ImageResource = ImageResource(downloadURL: url, cacheKey: item.photoURL)

KFImage(source: .network(resource))
    .resizable()
    .aspectRatio(contentMode: .fill)
    .clipped()
    .progressViewStyle(.linear)
    .frame(width: size, height: size)

UIKit

capturedImageView.kf.setImage(with: imageURL, placeholder: nil, options: [
    .scaleFactor(UIScreen.main.scale),
    .transition(.fade(0.25)),
    .targetCache(ImageCache(name: "offline")),
 ]) 
1

There are 1 best solutions below

0
Nadi Hassan On

My work around:

func customKFImage(_ url: URL) -> KFImage {
        let result = KFImage(url)
        result.options = KingfisherParsedOptionsInfo(
            KingfisherManager.shared.defaultOptions + [.loadDiskFileSynchronously, .targetCache(AppCommon.KFCache)])
        return result
 }