【问题标题】:Trying to load images from firebase separately from the rest of the data in a tableview尝试从firebase加载图像与tableview中的其余数据分开
【发布时间】:2019-01-22 11:04:22
【问题描述】:

我有一个表格视图,其中包含从 Firebase Real-time(用于文本)和 Firebase Storage(用于图像)填充的一堆单元格。我整理的代码会等待所有文本和图像下载完毕,然后再呈现表格。

我希望文本首先加载到单元格中,然后是相关图像。

代码设置如下:

  1. 主视图控制器
  2. 存储标签和图像视图的 Tableviewcell 文件。
  3. 模型文件。

给我问题的主要功能如下:

    func LoadDataFromImageTest() {


    databaseReference = Database.database().reference()

    let refPVV = Database.database().reference(withPath: "PVV").queryOrdered(byChild: "Status").queryEqual(toValue: "Active")

    refPVV.observeSingleEvent(of: .value, with: { [weak self] (snapshot) in

        //if the reference have some values
        if snapshot.childrenCount > 0 {

            //clearing the list
            self?.FBImageList.removeAll()

            //iterating through all the values
            for PVV in snapshot.children.allObjects as! [DataSnapshot] {
                //getting values
                let PVVObject = PVV.value as? [String: AnyObject]
                let PVVImageName  = PVVObject?["ImageName"] as! String?
                let PVVName = PVVObject?["Name"] as! String?

                let imageURL = Storage.storage().reference().child(PVVImageName!)

                imageURL.downloadURL(completion: { (url, error) in

                    if error != nil {
                        print(error?.localizedDescription as Any)
                        return
                    }

                    URLSession.shared.dataTask(with: url!, completionHandler: { (data, response, error) in

                        if error != nil {
                            print(error as Any)
                            return
                        }

                        guard let imageData = UIImage(data: data!) else { return }

                        DispatchQueue.main.async {



                            let PVV = FBImageModel(ImageName: imageData as UIImage?, Name: PVVName as String?)
                            self!.FBImageList.insert(PVV, at: 0)

                        }

                        DispatchQueue.main.async {
                            //reloading the tableview
                            self?.FBImageTableView.reloadData()
                        }

                    }).resume()
                })
            }
        }
    }
)}

必须如何更改才能在为所有单元格填充文本标签后单独加载图像?

提前感谢您的帮助。

【问题讨论】:

  • 尝试使用SDWebImage
  • 在这种情况下我该怎么做?

标签: swift image firebase uitableview


【解决方案1】:

您可以使用 FirebaseUI 来做到这一点。安装 FirebaseUI Pod

pod 'FirebaseUI/Storage'

然后使用下面的代码

 // Reference to an image file in Firebase Storage
    let reference = storageRef.child("images/stars.jpg")


    // Placeholder image
    let placeholderImage = UIImage(named: "placeholder.jpg")

    // Load the image in your imageView using SDWebImage
    imageView.sd_setImage(with: reference, placeholderImage: placeholderImage)

在您的场景中,不要下载 imageData。只需更改您的图像模型以存储存储引用,并在创建行时,使用sd_setImage 方法填充图像

【讨论】:

  • 阿杰,按照你的建议,我该去哪里做? 1.在我上面引用的函数中? 2.在tableviewcell文件中? 3.在tableview的cellForRowAt函数中?
  • 您可以在上面引用的函数中设置对 FBImageList 的引用。并在 cellForRow 函数中使用 sd_setimage 方法和参考
  • 或者您可以使用 imageName let imageURL = Storage.storage().reference().child(PVVImageName!) 直接在 cellForRowAtIndexpath 中创建引用。
  • 阿杰,感谢您的提示。我在你的帮助下成功了!我首先抓取了 URL,将其添加到主函数中的模型中。然后 cellForRowAtIndexpath 函数获取 URL,然后由 sd_setImage 使用。
  • 欢迎您@nm1213。如果它对你有帮助,那么答案可能值得竖起大拇指
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-15
  • 2017-11-01
  • 2019-05-25
  • 1970-01-01
  • 2011-10-19
相关资源
最近更新 更多