首页 > 解决方案 > 如何从 Firebase 数据库中获取图像路径以显示在我的 CollectionView 中?

问题描述

我想在我的 CollectionView 中显示 Firebase 数据库中的图像。我试过但没有结果。如果您想了解更多信息,请询问我。

Firebase 数据库截图

这是我的图像参考,我从 firebase 下载单个图像。

import Foundation
import UIKit
import FirebaseStorage

struct Utility {

    let storageRef = Storage.storage().reference(forURL: "gs://****-******.appspot.com/")
    func getImage(withName name: String, completion: @escaping (UIImage?) -> ()) {
        let imageRef = storageRef.child("images").child(name)
        imageRef.getData(maxSize: 2 * 1024 * 1024) { (data, error) in
            if error == nil && data != nil {
                let image = UIImage(data: data!)
                completion(image)
            } else {
                completion(nil)
            }
        }
    }
}

这是我的 FIREBASE 字典,我从数据库中检索信息。

import Foundation

class Place {
    let kImageName = "imageName"

    var imageName: String!

    init(withDictionary dict: [String : Any]) {
        self.imageName = dict[kImageName] as? String
    }
}

这是我的视图控制器,我想在 TableViewCell 内的 CollectionView 中显示我的数组图像。

import UIKit
import AVFoundation
import FirebaseDatabase
import FirebaseStorage

class GuidaTuristicaCollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var imagePlaces: UIImageView!

    var place: Place! {
        didSet {
            if let place = place {
                Utility().getImage(withName: place.imageName!, completion: { (image) in
                    DispatchQueue.main.async {
                        self.imagePlaces.image = image
                    }
                })
            }
        }
    }
}

class GuidaTuristicaTableViewCell: UITableViewCell {

    @IBOutlet weak var collectionView: UICollectionView!

}

class GuidaTuristicaViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate {

    var place: Place?

    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.delegate = self
        tableView.dataSource = self
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! GuidaTuristicaTableViewCell

        cell.collectionView.dataSource = self
        cell.collectionView.delegate = self
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 1
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! GuidaTuristicaCollectionViewCell
        cell.place = place
        return cell
    }
}

标签: arraysswiftuitableviewfirebase-realtime-databaseuicollectionview

解决方案


推荐阅读