arrays - 如何从 Firebase 数据库中获取图像路径以显示在我的 CollectionView 中?
问题描述
我想在我的 CollectionView 中显示 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
}
}
解决方案
推荐阅读
- python - 部署 Scrapy 时出现 ValueError
- c# - LDAPS DirectorySearcher.FindOne 抛出异常
- excel - Excel 自动编号与基于其他列的级别
- python - 强化算法似乎在学习,但脚本卡住了,代理没有重置
- ruby-on-rails - 为什么 webpacker:compile 在 javascript_pack_tag 中插入特定的主机名?
- domain-driven-design - 按 id 检索实体引用其他聚合
- c# - 循环遍历类属性并获得不同的值
- python - 将 csv 列的某些值替换为另一个列中的值
- ruby-on-rails - 满足条件后如何自动更新父级?
- bash - 根据索引从数组中删除多个元素