【问题标题】:Swift loading images from firebase从 Firebase 快速加载图像
【发布时间】:2017-10-02 14:36:51
【问题描述】:

有人知道为什么这不起作用吗?即使数据库和存储中有项目,tableView 也是空的并且不显示任何内容。在我实现从存储中加载图像之前,这很好用) 但是,如果我现在将其取出,它将无法访问recipeImage,因为它是在闭包中声明的。它不工作是因为它在关闭中吗?如果是这样,我该如何解决?

let parentRef = Database.database().reference().child("Recipes")
    let storage = Storage.storage()

    parentRef.observe(.value, with: { snapshot in

        //Processes values received from server
        if ( snapshot.value is NSNull ) {

            // DATA WAS NOT FOUND
            print("– – – Data was not found – – –")

        } else {

            //Clears array so that it does not load duplicates
            food = []

            // DATA WAS FOUND
            for user_child in (snapshot.children) {

                let user_snap = user_child as! DataSnapshot
                let dict = user_snap.value as! [String: String?]

                //Defines variables for labels
                let recipeName = dict["Name"] as? String
                let recipeDescription = dict["Description"] as? String
                let downloadURL = dict["Image"] as? String

                let storageRef = storage.reference(forURL: downloadURL!)

                storageRef.getData(maxSize: 1 * 1024 * 1024) { (data, error) -> Void in

                    let recipeImage = UIImage(data: data!)

                    food.append(Element(name: recipeName!, description: recipeDescription!, image: recipeImage!))
                }
            }
            self.tableView.reloadData()
        }
    })

【问题讨论】:

  • 如果你在 self.tableView.reloadData() 之后移动 food.append(Element(name: recipeName!, description: recipeDescription!, image: recipeImage!)) ,看看是什么行为?
  • 它工作!非常感谢
  • 太好了,我已将其添加为答案
  • @3stud1ant3 hmm... 刚发现一个问题,tableView 中的item 会随机重新加载,如果重新加载view,它们的顺序不一样,你知道如何解决这个问题吗?跨度>
  • 一种方法是在重新加载 tableview 之前进行排序,

标签: swift firebase firebase-realtime-database firebase-storage


【解决方案1】:

移动

self.tableView.reloadData()

之后

food.append(Element(name: recipeName!, description: recipeDescription!, image: recipeImage!))

【讨论】:

    猜你喜欢
    • 2021-05-27
    • 1970-01-01
    • 1970-01-01
    • 2018-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多