繁体   English   中英

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

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

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

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

我在Cell中添加了一个UIUmage。

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

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

没有成功

这是完整的功能,textlabel正确显示,但不是图像。

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中执行此PostCell ,您应该为其创建函数,并从cellForRowAt调用该函数

EDIT3:另外要访问FirebaseStorageUI您需要将其添加到podfile并运行pod install

pod 'FirebaseUI/Storage'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM