如何在 ios (swift) 的 tablecell 中从 firebase 读取图像?

How to read an image from firebase in tablecell on ios (swift)?

我正在尝试读取存储在我的 firebase 存储中的一些图像。

这怎么可能实现?我正在使用带有 Cell 的 UITable 视图。

我在单元格中添加了一个 UIUmage。

然后我尝试通过以下方式从存储中调用图像:

cell?.imageView?.image = UIImage(named: "gs://mybundefirebase.appspot.com")

没有成功

这是完整的功能,文本标签正确显示,但图像不正确。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "PostCell")
    cell?.textLabel?.text = postData[indexPath.row];
    cell?.textLabel?.textAlignment = .center;
    cell?.imageView?.image = UIImage(named: "gs://mybundefirebase.appspot.com")

    return cell!
}

任何帮助...都会很棒!我遇到了很多麻烦!!

谢谢大家!!

您可以像这样使用您导入的 Firebase: FirebaseStorageUI

import FirebaseStorageUI

和这样的用户:

let urlToData = "gs://mybundefirebase.appspot.com" + "/" + folderOfImage + "/" + imageNameOnStorage 

let reference = FIRStorage.storage().reference(forURL: urlToData) //ctreates reference to image in storage

let placeholderImage = #imageLiteral(resourceName: "placeholder") //placeholder if wanted

imageView.sd_setImage(with: reference, placeholderImage: placeholderImage) //will set your image for you

这还将为您将图像缓存在内存中,因此如果您计划在应用程序的其他位置检索相同的图像,您也不必对存储进行多次请求

编辑:在您的情况下,您可以让 urlToData 等于:

let urlToData = "gs://mybundefirebase.appspot.com/a-chair-bl.jpg"

如果存储图像的名称是 a-chair-bl.jpg 并且它被放置在 root 文件夹中,这将起作用

EDIT2:您可以在 cellForRowAt 中调用上面的代码,但我不建议这样做。您应该在 PostCell 中创建一个函数,然后从 cellForRowAt

中调用该函数

EDIT3:同样要访问 FirebaseStorageUI,您需要将此添加到您的 podfile 和 运行 pod install

pod 'FirebaseUI/Storage'