【问题标题】:SDWebImage Does Not Accept StorageReference from Firebase StorageSDWebImage 不接受来自 Firebase 存储的 StorageReference
【发布时间】:2018-06-19 23:07:52
【问题描述】:

我正在尝试在 UITableView 上显示来自 Firestore 和 Google Storage 的数据。

我设置了一个自定义单元格,它有自己的名为“PriceGuideCell”的类

在安装 FirebaseUI/Storage pod 后,我尝试使用 Firebase 网站上显示的方法。

 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
                let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! PriceGuideCell
                var item = itemData[indexPath.row]

      let reference: StorageReference = storageRef.child("test/\(item.itemID!).jpg")

                    cell.nameLabel?.text = item.name
                    cell.yearLabel?.text = String(item.year)
                    cell.boxnumLabel?.text = String(item.boxnum)
                    cell.itemImage?.sd_setImage(with: reference, placeholderImage: #imageLiteral(resourceName: "placeholder"))

                }
            }

            return cell
    }

上面的代码给了我一个错误:

Cannot convert value of type 'StorageReference' to expected argument type 'URL'

下面的代码,它使用了(reference).downloadURL,我已经开始工作了,但我不知道这是否是最佳实践:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! PriceGuideCell
            var item = itemData[indexPath.row]

            let imageURLRef = storageRef.child("test/\(item.itemID!).jpg")

            imageURLRef.downloadURL { url, error in
            if let error = error {
                print("There's an error:\(error)")
            } else {

                cell.nameLabel?.text = item.name
                cell.yearLabel?.text = String(item.year)
                cell.boxnumLabel?.text = String(item.boxnum)
                cell.itemImage?.sd_setImage(with: url, placeholderImage: #imageLiteral(resourceName: "placeholder"))


            }
        }

        return cell
    }

我还需要安装什么东西让 sd_setImage 接受 StorageReference 吗?另外,我的 Podfile 的目标是 9.0。 in the FirebaseUI readme 关于使用 8.0.我不知道这是否会影响它,但是对于我的项目使用的其他一些 pod,8.0 太低了。

【问题讨论】:

  • 打印 (imageURLRef),看到它打印正确的 URL 了吗?
  • 我得到 gs://projectname-1c14c.appspot.com/test/13377.jpg
  • 别忘了导入 FirebaseStorageUI。
  • 现在是 ** FirebaseUI**。而且你必须在你的 pod 文件中使用_frameworks。

标签: swift firebase-storage firebaseui sdwebimage


【解决方案1】:

导入 FirebaseUISDWebImage

import FirebaseUI
import SDWebImage

Firebase 的 StorageReference 将使用以下方法:

UIImageView.sd_setImage(with: StorageReference, placeholderImage: UIImage)

如下所示:

self.imageView.sd_setImage(with: Storage.storage().reference().child(url),
                           placeholderImage: nil)

你必须在 Podfile 中有

pod 'FirebaseUI/Firestore'
pod 'FirebaseUI/Storage'

(或其他组合,如此处所述https://github.com/firebase/FirebaseUI-iOS

与!

你必须

use_frameworks!

令人困惑的是,一些 Firebase doco (2021) 声明您应该避免使用 use_frameworks!

但是,如果没有 use_frameworks!,FirebaseUI 将工作。

【讨论】:

  • 尝试此操作会产生无法构建 Objective-C 模块“FirebaseUI”?
【解决方案2】:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
                let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! PriceGuideCell
                var item = itemData[indexPath.row]

      let referenceImage: StorageReference = storageRef.child("test/\(item.itemID!).jpg")
      let url = URL(string: referenceImage!)!
                    cell.nameLabel?.text = item.name
                    cell.yearLabel?.text = String(item.year)
                    cell.boxnumLabel?.text = String(item.boxnum)
                    cell.itemImage?.sd_setImage(with: url, placeholderImage: #imageLiteral(resourceName: "placeholder"))

                }
            }

            return cell
    }

【讨论】:

  • 我发布的第二个代码块有效,第一个无效,我认为应该有效。出于某种原因,SDWebImage 不接受 StorageReference,即使 Firebase 说它应该可以工作。请参阅他们网站的Downloading Images with FirebaseUI 部分。
  • 检查答案打印网址
  • 通过调用正确的模块解决了问题。导入 FirebaseStorageUI。哦!感谢 Khawar 的帮助;结果只是一个简单的错误。
  • 无法将“StorageReference”类型的值转换为预期的参数类型“String”
猜你喜欢
  • 2023-03-05
  • 1970-01-01
  • 2021-02-22
  • 2016-07-14
  • 2017-06-23
  • 2018-05-05
  • 2023-03-22
  • 2018-08-13
  • 2020-08-01
相关资源
最近更新 更多