【发布时间】: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