【问题标题】:Alamofire: Do af_setImageWithURL and ImageDownloader share the same AutoPurgingImageCache?Alamofire:af_setImageWithURL 和 ImageDownloader 共享同一个 AutoPurgingImageCache 吗?
【发布时间】:2016-01-24 12:22:01
【问题描述】:

我在我的项目中使用 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

【问题讨论】:

    标签: ios swift caching uiimageview alamofire


    【解决方案1】:

    很好的问题。答案是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 实例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-10
      • 1970-01-01
      • 2012-01-16
      • 2019-04-02
      • 1970-01-01
      • 2011-08-27
      • 2012-03-24
      相关资源
      最近更新 更多