【问题标题】:Cache Image from Firebase从 Firebase 缓存图像
【发布时间】:2017-07-14 17:32:48
【问题描述】:

我有多个单元格当前保存来自 Firebase 的不同照片。每次用户加载这些图像然后滚动时,它们都会重新下载,这会快速消耗数据。我发现这与任何拥有计量数据计划的用户有关。我能做些什么来解决这个问题? Firebase 是否提供任何选项来缓存下载的图像?

这就是我当前将图像调用到单元格中的方式:

if let imageName = post["image"] as? String {
        let imageRef = FIRStorage.storage().reference().child("images/\(imageName)")
        imageRef.data(withMaxSize: 25 * 1024 * 1024, completion: { (data, error) -> Void in if error == nil {
            let image = UIImage(data: data!)
            cell.postImageView.image = image

【问题讨论】:

    标签: ios swift caching firebase firebase-storage


    【解决方案1】:

    使用Kingfisher 缓存图像。它很轻,很容易使用。只需从 firebase 传递您的 url,它就会自动缓存它。

    let url = URL(string: "url_of_your_image")
    imageView.kf.setImage(with: url)
    

    【讨论】:

    • 模棱两可的答案。您将从 Firebase 存储中传递什么 URL?它返回.fullPath,它不是一个有效的 URL。还有什么?
    • 来自 chaets sheet github.com/onevcat/Kingfisher/wiki/… 但是你应该写一点逻辑
    【解决方案2】:

    您也可以使用Alamofire。虽然它不会自动处理缓存,但与 Kingfisher 相比,它能够处理几乎所有类型的网络需求。

    PS:是的,我知道 - 通常 - 如果我使用 Firebase,我不需要任何网络功能。

    但是,例如;由于 Firebase 数据库和 Firestore 无法处理全文搜索,您需要使用第三方解决方案,因此,您有时可能需要功能齐全的网络实用程序。

    【讨论】:

      【解决方案3】:

      Firebase 在从服务器获取实时数据之前已经在本地缓存了数据库,因此问题没有那么严重。但是如果你想做更好的缓存,请使用Glide,Glide 会缓存图像,你可以指定时间签名,以便它仅在图像更新时重新获取图像。

      这非常简单,但您确实需要将图像托管在 google 云服务、aws 或什至任何地方

       ImageView imageView = (ImageView) findViewById(R.id.my_image_view);
       Glide.with(this).load("url").into(imageView);
      

      【讨论】:

      • 编辑:Glide 仅适用于 Android
      • 哎呀,没注意到你在使用 ios
      猜你喜欢
      • 1970-01-01
      • 2023-03-20
      • 2018-03-02
      • 2019-05-03
      • 2019-06-11
      • 2016-10-08
      • 2017-05-31
      • 1970-01-01
      • 2018-09-11
      相关资源
      最近更新 更多