【问题标题】:How to use SDWebImageManager to download image only if NOT already cached?仅在尚未缓存的情况下如何使用 SDWebImageManager 下载图像?
【发布时间】:2018-02-21 12:55:41
【问题描述】:

我需要 SDWebImageManager 的完成处理程序功能(设置下载或缓存的黑白图像),所以我使用它而不是 sd_setimage。我的问题是我不知道如何使用 SDWebImageManager 下载或获取图像的缓存版本。每次表格视图单元出列并重新加载时,图像都会重新下载。我尝试设置选项:SDWebImageDownloaderOptions.useNSURLCache,但无济于事。任何建议将不胜感激!这是我的代码:

SDWebImageManager.shared().imageDownloader?.downloadImage(with:URL(string: imgURL), options: SDWebImageDownloaderOptions.useNSURLCache, progress: nil, completed: { (image, error, cacheType, url) in 
   if image != nil {
      let beginImage = CIImage(image: image!)
      let blackNwhiteImg = beginImage?.applyingFilter("CIColorControls", withInputParameters: [kCIInputSaturationKey:0.0])
      let newImage = UIImage(ciImage: blackNwhiteImg!)
      cell.button.setImage(newImage, for: .normal)
   }
})

【问题讨论】:

    标签: ios swift caching sdwebimage


    【解决方案1】:

    从缓存中提取的更新答案:

    SDWebImageManager.shared().loadImage(with: URL?, options: SDWebImageOptions, progress: { (Int, Int, URL?) in
        code
    }, completed: { (UIImage?, Data?, Error?, SDImageCacheType, Bool, URL?) in
        code
    })
    

    为了参考,我附上这个屏幕截图,显示 XCode 在输入函数时呈现的注释:

    另外,SDWebImageManager 文件中包含的 cmets:

    /**
     * Downloads the image at the given URL if not present in cache or return the cached version otherwise.
     *
     * @param url            The URL to the image
     * @param options        A mask to specify options to use for this request
     * @param progressBlock  A block called while image is downloading
     *                       @note the progress block is executed on a background queue
     * @param completedBlock A block called when operation has been completed.
     *
     *   This parameter is required.
     * 
     *   This block has no return value and takes the requested UIImage as first parameter and the NSData representation as second parameter.
     *   In case of error the image parameter is nil and the third parameter may contain an NSError.
     *
     *   The forth parameter is an `SDImageCacheType` enum indicating if the image was retrieved from the local cache
     *   or from the memory cache or from the network.
     *
     *   The fith parameter is set to NO when the SDWebImageProgressiveDownload option is used and the image is
     *   downloading. This block is thus called repeatedly with a partial image. When image is fully downloaded, the
     *   block is called a last time with the full image and the last parameter set to YES.
     *
     *   The last parameter is the original image URL
     *
     * @return Returns an NSObject conforming to SDWebImageOperation. Should be an instance of SDWebImageDownloaderOperation
    

    【讨论】:

    • 非常感谢您的回复。这与我发布的代码完全相同。在我看来,您的代码和我的代码都在访问相同的 .downloadImage() 函数。你能更具体地使用你的语法吗?有没有我没有设置的选项?我尝试了有无选项:SDWebImageDownloaderOptions.useNSURLCache。再次感谢。
    • 我的情况的另一个细节是,我注意到图像在出列的 tableview 单元格中重新加载。但是根据我对 sdwebimage 的理解,出列和重新加载的单元格应该与缓存的图像无缝地重新加载。
    • 每次加载图像时,我都会在完成块中打印 cacheType 值。每次都是零。这似乎说明没有缓存图片?
    • @DanielGrigsby 更新了我的答案,我从错误的部分中提取了代码并进行了相应更新,并包含了一些支持文档。
    • 太棒了。非常感谢!
    【解决方案2】:

    您可以使用SDWebImageManager.shared().cachedImageExists(for: imageUrl) 来了解URL 是否被缓存

    之后就可以通过这个方法获取cacheKey了

    let cacheKey = SDWebImageManager.shared().cacheKey(for: imageUrl)
    

    拥有 CacheKey 你可以调用这个方法,在他的闭包中你会得到你的缓存图片

    SDWebImageManager.shared().imageCache.queryDiskCache(forKey: cacheKey, done: { (image, cacheType) in
    
                    })
    

    【讨论】:

    • 感谢您的回复。然后如何在完成块中获取该 URL 的缓存图像?据我所知,没有传入“图像”值。
    • 感谢您的回答。了解 CacheKey 非常有用。我认为 .loadImage 是我一直在寻找的。​​span>
    猜你喜欢
    • 2018-05-08
    • 1970-01-01
    • 2012-11-02
    • 1970-01-01
    • 2012-06-23
    • 1970-01-01
    • 2013-05-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多