SDWebImage Download image and store to cache for key

后端 未结 11 1530
失恋的感觉
失恋的感觉 2020-12-24 03:30

Hello I am using the SDWebImage framework in a project and I want to download and cache images, but I think my code is storing an image in the cache twice? Is there any way

11条回答
  •  鱼传尺愫
    2020-12-24 04:02

    Yes is possible to download the image using SDWebImage And store into local memory Manually.

    Downloaded Image store into local memory using SDWebImage

    func saveImage(url: URL, toCache: UIImage?, complation: @escaping SDWebImageNoParamsBlock) {
        guard let toCache = toCache else { return }
    
        let manager = SDWebImageManager.shared()
        if let key = manager.cacheKey(for: url) {
            manager.imageCache?.store(toCache, forKey: key, completion: complation)
        }
    }
    

    Load image from memory using image URL

    static func imageFromMemory(for url: String) -> UIImage? {
        if let encoded = url.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed),
            let url = URL(string: encoded) {
            let manager = SDWebImageManager.shared()
            if let key: String = manager.cacheKey(for: url),
                let image = manager.imageCache?.imageFromMemoryCache(forKey: key) {
                return image
            }
        }
        return nil
    }
    

提交回复
热议问题