Kingfisher 自定义缓存机制

自定义管理缓存的单利:

import Kingfisher
import UIKit

class CustomKinfinsherCache {
    static let share:CustomKinfinsherCache = CustomKinfinsherCache()
    let cache:ImageCache = try! ImageCache.init(name: "Test", cacheDirectoryURL: CustomKinfinsherCache.getRootPath()) { (url, cache) in
        var testUrl = url
        testUrl.appendPathComponent(cache)
        return testUrl
    }
    
    
    init() {
        self.cache.diskStorage.config.expiration = .never
        print("==== \(self.cache.diskStorage.config)")
    }
    
   static func getRootPath() -> URL {
        
        let dstPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
        print("dstPath === \(dstPath)")
        return URL.init(fileURLWithPath: dstPath)
    }

    
}


使用如下:

let url = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fnimg.ws.126.net%2F%3Furl%3Dhttp%253A%252F%252Fdingyue.ws.126.net%252F2021%252F0611%252F2e84f869j00qujnx3001nc000hs00hsc.jpg%26thumbnail%3D650x2147483647%26quality%3D80%26type%3Djpg&refer=http%3A%2F%2Fnimg.ws.126.net&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1636343813&t=b8c3cae41cc8ef52bd071fe53052ab44"
        
        
//        let processor = DownsamplingImageProcessor(size: CGSize.init(width: 50, height: 50))
//                     |> RoundCornerImageProcessor(cornerRadius: 0)
        
        let op:KingfisherOptionsInfo = [
//            .processor(processor),
                                        .cacheOriginalImage,.originalCache(CustomKinfinsherCache.share.cache),.targetCache(CustomKinfinsherCache.share.cache)]
        
        
        
        self.imgView.kf.setImage(with: URL.init(string: url), placeholder: nil, options: op, progressBlock: nil, completionHandler: nil)
        

你可能感兴趣的:(Kingfisher 自定义缓存机制)