Firebase Swift 3使用NSURLSession将配置文件图像加载到TableView中

时间:2017-01-18 13:01:01

标签: ios uitableview firebase swift3 nsurlsession

我正在按照教程加载图片网址并将其附加到数组中,以便在每个用户的UITableView单元格中显示用户个人资料图片,使用以下代码块:

let url = NSURL(string: urlString)
        NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler: { (data, response, error) in

        //download hit an error so lets return out
        if error != nil {
            print(error)
            return
        }

        dispatch_async(dispatch_get_main_queue(), {
            //"profilePics" is my array of UIImages made as a global var
            self.profileImages.append(UIImage[data!])
        })

    }).resume()

但出于某种原因" NSURLSession"在编程时它并不是一个建议,它会返回错误,所以我改变了#34; NSURL ..."简单地说" URL ..." :

                //Grabbing images from db
                print("starting get image block : ")
                    let url = URL(string: profileImageURL)
                    URLSession.shared.dataTask(with: url!, completionHandler: {(data, response, error) in
                        print("grabbing image: ")       //Test line for monitor
                        print(String(describing: data)) //Test line for monitor : returns "nil"
                        if error != nil{
                            print(error!)
                            return
                        }

                        DispatchQueue.main.async {
                            //"profilePics" is my array of UIImages made as a global var
                            self.profilePics.append(UIImage(data: data!))
                            print("Image appended")
                        }

                 }).resume()

                 // grabbing images from db end

但是每次运行这个时我得到的数据为nil,它永远不会附加到我的数组中。有人能解释我错过的东西吗?这是我第一次使用Firebase。

1 个答案:

答案 0 :(得分:0)

我实际上发现了问题所在,并且我不想在其他人可能正在寻找同一问题的解决方案的情况下删除帖子。事实证明,URLSession工作正常,因此找不到“NSURLSession”不是问题。但是,一旦我完成了工作,配置文件图像只会在重新加载我的uitableview后加载。一旦我移动我的tableview.reload()它就一直工作,这是我的Swift 3的工作代码:

//This method grabs all user data for each user & adds them to an 
//array to be displayed in a uitableview
 func getUsersData(){
    FIRDatabase.database().reference().child("users").observe(.childAdded, with: {(snapshot) in

        if let dictionary = snapshot.value as? [String:AnyObject]{

            print(snapshot.childrenCount)

            if (snapshot.childrenCount > 2) {

                self.users.append(dictionary["name"] as! String)

                if !(dictionary["subjects"]?.isEqual(""))!{

                    self.subjects.append(dictionary["subjects"] as! String)

                }

                else{

                    self.subjects.append("N/A")

                }

                if !(dictionary["location"]?.isEqual(nil))!{

                    self.states.append(dictionary["location"] as! String)

                }

                else{

                    self.states.append("N/A")

                }

                self.ratings.append("4.1")
                if let profileImageURL = dictionary["userPhoto"] {

                    let url = URL(string: profileImageURL as! String)
                    URLSession.shared.dataTask(with: url!, completionHandler: {(data, response, error) in

                        if error != nil{

                            print(error!)
                            return

                        }

                        DispatchQueue.main.async {

                            self.profilePics.append(UIImage(data: data!))
                            print("Image appended")

                        }

                        // This line is required to reload with new data in tableview array data from db

                        DispatchQueue.main.async{

                            self.browseUITableView.reloadData()

                        }

                    }).resume()

                }
                // grabbing images from db end
            }
        }
    })
}