ios - 如何在 ios (swift) 的表格单元格中从 firebase 读取图像?

标签 ios swift database firebase storage

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

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

我在 Cell 中添加了一个 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!
}

任何帮助...都将是不可思议的!我遇到了很多麻烦!!

谢谢大家!!

enter image description here

最佳答案

您可以使用 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'

关于ios - 如何在 ios (swift) 的表格单元格中从 firebase 读取图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42000099/

相关文章:

ios - 如何在 Windows 上调试 iPad Flash 应用程序?

ios - Swift 中的持续动画 View

mysql - 如何为基于邮政编码的服务设计 MySQL 数据库?

mysql - 使用 PostgreSQL 模式文件将 mySQL 数据移动到 postgres

ios - Swift 中的结构问题以及从 url 制作 UIImage 的问题?

ios - 使用 pragma ifdef 有条件地为 mac 和 ios 编译相同的协议(protocol)头?

ios - 是否可以阅读当前在 iOS 7 中配置蓝牙通知的方式?

swift - RxSwift 通过 flatMaps 链传播一个值

swift - 如果我知道当前日期,如何确定一周的接下来几天?

database - 如果 Cassandra 节点在压缩期间用完磁盘空间会怎样?