簡體   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