如何从Firebase数据库获取图像路径以显示在CollectionView中?

时间:2019-01-29 17:43:37

标签: arrays swift uitableview firebase-realtime-database uicollectionview

我想在我的CollectionView中显示Firebase数据库中的图像。 我已经尝试过,但是没有结果。如果您需要更多信息,请问我。

Firebase database screenshot

这是我的图像参考 我从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
    }
}

0 个答案:

没有答案