ios - 如何仅在尚未缓存的情况下使用 SDWebImageManager 下载图像?

标签 ios swift caching sdwebimage

我需要 SDWebImageManager 的完成处理程序功能(将下载或缓存的图像设置为黑白),所以我使用它而不是 sd_setimage。我的问题是我不知道如何使用 SDWebImageManager 下载或获取图像的缓存版本。每次 tableview 单元格出列并重新加载时,图像都会重新下载。我尝试设置选项: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)
   }
})

最佳答案

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

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

为了引用起见,我附上了这张显示 XCode 在进入函数时显示的注释的屏幕截图:

enter image description here

此外,SDWebImageManager 文件中包含的注释:

/**
 * 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

关于ios - 如何仅在尚未缓存的情况下使用 SDWebImageManager 下载图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46186928/

相关文章:

ios - 如何从 viewController 访问 Swift 3 中的另一个 viewController

swift - Swift 3 中的 UIView isKindOfClass

android - Android中如何实现json的离线缓存?

objective-c - UIAlertView clickedButtonAtIndex 未调用

ios - 如何在 UITextField 扩展中创建一个新字段?

ios - 核心数据和数据完整性 : read operations vs write operations. 如何保护?

java - Spring : Spring-cache not working

iphone - 如何在 iOS 中使用 ASI Http 将数据发布到 Web 服务?

ios - 在 Alamofire POST 方法中发布多个 json 对象 - Swift/IOS

mysql - -编辑-我无法通过重新启动服务器或使用多个查询来清除MySQL查询缓存