ios - Alamofire:af_setImageWithURL 和 ImageDownloader 是否共享同一个 AutoPurgingImageCache?

标签 ios swift caching uiimageview alamofire

我在我的项目中经常使用 AlamofireImage 将图像设置为 UIImageView:

let imageView = UIImageView(frame: frame)
let URL = NSURL(string: "https://httpbin.org/image/png")!

imageView.af_setImageWithURL(URL)

在我的应用程序中的某个时刻,我需要直接获取图像,因为除了将它设置为 UIImageView 之外,我还想做其他事情。 AlamofireImage 为这个用例提供了 ImageDownloader:

let downloader = ImageDownloader()
let URLRequest = NSURLRequest(URL: NSURL(string: "https://httpbin.org/image/jpeg")!)

downloader.downloadImage(URLRequest: URLRequest) { response in
    print(response.request)
    print(response.response)
    debugPrint(response.result)

    if let image = response.result.value {
        print(image)
    }
}

这两种情况是否共享相同 AutoPurgingImageCache

最佳答案

好问题。答案是NO,它们不共享相同的AutoPurgingImageCache

如果您看一下 ImageDownloader 初始化器,您会发现它会自动创建一个自定义的 AutoPurgingImageCache

public init(
    configuration: NSURLSessionConfiguration = ImageDownloader.defaultURLSessionConfiguration(),
    downloadPrioritization: DownloadPrioritization = .FIFO,
    maximumActiveDownloads: Int = 4,
    imageCache: ImageRequestCache? = AutoPurgingImageCache())
{
    self.sessionManager = Alamofire.Manager(configuration: configuration)
    self.sessionManager.startRequestsImmediately = false

    self.downloadPrioritization = downloadPrioritization
    self.maximumActiveDownloads = maximumActiveDownloads
    self.imageCache = imageCache
}

如果您希望共享同一张图片,则需要在 UIImageView.af_sharedImageDownloader 实例上下载您的图片。第二种方法是使用 UIImageView.af_sharedImageDownloader 上的 AutoPurgingImageCache 属性创建第二个 ImageDownloader 实例。

关于ios - Alamofire:af_setImageWithURL 和 ImageDownloader 是否共享同一个 AutoPurgingImageCache?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33328179/

相关文章:

ios - 在ObjectiveC中使用FBSDK框架的FBSDKInternalUtility异常

swift - GCD 真的是线程安全的吗?

actionscript-3 - 如何在AS3中缓存音频流数据并访问它

ios - 后退按钮显示在 Root View Controller 上

php - 检测缓存驱动是否支持标签

java - 如何在不同 Camel 路线上的方法之间共享对象

php - 为什么我的 php json_encode 不能在 alamofire 中使用 .responseJSON 序列化?

ios - UITableView带有Json文件,加载图像到单元格消失

ios - XCode - EXC_BAD_ACCESS。 -[非A型保留]

ios - 如何从不同 View 调用CoreLocation