swift - 从 Firebase 下载图片

标签 swift firebase-realtime-database uicollectionview uicollectionviewcell firebase-storage

请帮帮我。解释如何在单元格中设置图像。在我的数据库中,我有:标题、描述和 imageURL(来自 Firebase 存储的 url)。你能给我写一个代码并解释一下吗?

class TrainingProgram
{
    var description = ""
    var title = ""
    var imageURL = ""


    init(description: String, title: String, imageURL: String) {
        self.description = description
        self.title = title
        self.imageURL = imageURL

    }

从 firebase 获取数据的函数。

func fetchPrograms() {
        Database.database().reference().child("programs").observe(.childAdded) { (snapshot) in
            if let dict = snapshot.value as? [String: AnyObject] {
                let newTitle = dict["title"] as! String
                let newDescription = dict["description"] as! String
                let newImageURL = dict["imageURL"] as! String
                let trainingCell = TrainingProgram(description: newDescription, title: newTitle, imageURL: newImageURL)
                self.trainingPrograms.append(trainingCell)
                DispatchQueue.main.async {
                    self.collectionView.reloadData()

                }
            }
        }

    }

这就是我为我的单元格设置标题和描述的方式。

 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
    {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TrainingProgramCollectionViewCell", for: indexPath) as!  TrainingProgramCollectionViewCell

        //cell.featuredImageView.setImage(from: indexPath.item)
        cell.titleLabel.text = trainingPrograms[indexPath.item].title
        cell.descriptionLabel.text = trainingPrograms[indexPath.item].description

我在接收数据获取图片的函数中需要写些什么以及如何在单元格中设置图片

structureOfDatabase

最佳答案

从此链接安装 SDWebImage pod https://github.com/SDWebImage

并在您的 CollectioView cellForRow 方法中

    var images_list = [String]()
    var imagesArray = [URL]()
    images_list.append(itemModel[indexPath.row]. imageURL)
    let storage = Storage.storage().reference()

    for x in images_list{
        print(x)

        let storageRef = storage.child("images/\(x).jpg")
        storageRef.downloadURL { (url, error) in
            if let error = error{
                print(error.localizedDescription)
            }
            else{
                imagesArray.append(url!)
            }
            if let x = images_list.last{
              cell.itemImage.sd_setImage(with: URL(string: x), placeholderImage: UIImage(named: "default"))
            }
        }
    }

完整代码

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
   {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TrainingProgramCollectionViewCell", for: indexPath) as!  TrainingProgramCollectionViewCell

    //cell.featuredImageView.setImage(from: indexPath.item)
    cell.titleLabel.text = trainingPrograms[indexPath.item].title
    cell.descriptionLabel.text = trainingPrograms[indexPath.item].description

    var images_list = [String]()
    var imagesArray = [URL]()
     for x in images_list{
        print(x)

        let storageRef = storage.child("images/\(x).jpg")
        storageRef.downloadURL { (url, error) in
            if let error = error{
                print(error.localizedDescription)
            }
            else{
                imagesArray.append(url!)
            }
            if let x = images_list.last{
              cell.itemImage.sd_setImage(with: URL(string: x), placeholderImage: UIImage(named: "default"))
            }
        }
    }

 }

试试这个下载单张图片只需从 firebase 获取您的 imageUrl

 let x: String = "yourImageUrl" 
 let storageRef = storage.child("images/\(x).jpg")
        storageRef.downloadURL { (url, error) in
            if let error = error{
                print(error.localizedDescription)
            }
            else{
                imagesArray.append(url!)
            }
            if let x = images_list.last{
              cell.itemImage.sd_setImage(with: URL(string: x), placeholderImage: UIImage(named: "default"))
            }
        }

关于swift - 从 Firebase 下载图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56185143/

相关文章:

android - 无法将数据上传到 firebase 数据库?

javascript - Firebase 推送到实时数据库不起作用

Firebase实时数据库添加基于 child 的规则

ios - 为什么我的 uipagecontrol 第一次没有正确更新,但是第二次通过 Xcode 更新了

arrays - 在 Swift 中从 Firebase 填充的多维数组

ios - 无法将类型 'nil' 的值分配给类型 'NSMutableArray' 的值

ios - Swift:UIScrollViewDelegate 扩展

ios - UICollectionViewCell 不适合设备屏幕

ios - 根据 textView 调整单元格大小不起作用?

ios - 将 UILabel 放置在父级的中心