简体   繁体   中英

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

I'm trying to read some images stored in my firebase storage.

How is this possible to be achieve? I'm using a UITable View with Cell.

I've added a UIUmage in the Cell.

then I 'm trying to call the image from the storage via:

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

without success

here is the full function, the textlabel appear correctly, but not the image.

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!
}

any help ... will be incredible ! I'm having lot of trouble with that !!

Thanks guys !!

在此输入图像描述

You can use the Firebase: FirebaseStorageUI that you import like this

import FirebaseStorageUI

and user like this:

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

This will also cache the image in the memory for you so you won't have to make to many requests to the storage as well if you plan to retrieve the same image somewhere else in your app

EDIT: in your case you can have the urlToData be equal to:

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

This would work if the name of the image on storage is a-chair-bl.jpg and it is placed in the root folder

EDIT2: you can call the above code in cellForRowAt but I would not suggest this. You should do it in your PostCell where you should create a function for it and just call the function from cellForRowAt

EDIT3: Also to access FirebaseStorageUI you need to add this to your podfile and run pod install

pod 'FirebaseUI/Storage'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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