无法从FIRStorage下载用户个人资料图片

时间:2018-07-18 13:27:02

标签: swift xcode firebase firebase-storage

当用户转到“用户个人资料”标签时,他在应用中出错并崩溃,而我在URLSession.shared.dataTask中的Xcode中遇到错误

func setupProfile(){

    if Auth.auth().currentUser?.uid == nil{

        logout()
    }else{

        usrnNameButton.isHidden = false
        let uid = Auth.auth().currentUser?.uid
        databaseRef.child("Users").child(uid!).observeSingleEvent(of: .value,
                                                                  with: { snapshot in

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


                if let profileImageuRL = dict["pic"] as? String
                {
                    let url = URL(string: profileImageuRL)
                    URLSession.shared.dataTask(with: url!, completionHandler: { (data, response, error) in
                        if error != nil {
                            print(error!)
                            return
                        }

                        DispatchQueue.main.async {
                            self.PP.image = UIImage(data: data!)
                        }

                    }).resume()
                }
                if let coverImageuRL = dict["CoverPic"] as? String
                {
                    let url = URL(string: coverImageuRL )
                    URLSession.shared.dataTask(with: url!, completionHandler: { (data, response, error) in
                        if error != nil {
                            print(error!)
                            return
                        }

                        DispatchQueue.main.async {
                            self.coverPic.image = UIImage(data: data!)
                        }

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

这是我设置用户数据的代码
这样任何人都可以帮助我 谢谢

2 个答案:

答案 0 :(得分:0)

Xcode 8•Swift 3

如果需要,可以使用它下载图像

func downloadImage(url: URL) {
    print("Download Started")
    getDataFromUrl(url: url) { data, response, error in
        guard let data = data, error == nil else { return }
        print(response?.suggestedFilename ?? url.lastPathComponent)
        print("Download Finished")
        DispatchQueue.main.async() {
            self.imageView.image = UIImage(data: data)
        }
    }
}

用法:

    if let url = URL(string: "http://www.apple.com/euro/ios/ios8/a/generic/images/og.png") {
          downloadImage(url: url)
    }

答案 1 :(得分:0)

您好,感谢您重播,现在我们已修复错误,并且在按“个人资料”标签时应用仍然可以运行,但是个人资料图片没有空白图片{func setupProfile(){

    if Auth.auth().currentUser?.uid == nil{

        logout()
    }else{
        let changeRequest = Auth.auth().currentUser?.createProfileChangeRequest()
         //userName.text = changeRequest?.displayName
        usrnNameButton.isHidden = false
        let uid = Auth.auth().currentUser?.uid
        databaseRef.child("Users").child(uid!).observeSingleEvent(of: .value,
                                                                  with: { snapshot in

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


                if let profileImageuRL = dict["pic"] as? String
                {

                    //let url = URL(string: profileImageuRL)

                        //self.downloadImage(url: url!)

                            //self.PP.image = UIImage(data: data!)
                    //let resource = ImageResource(downloadURL: profileImageuRL, cacheKey: profileImageuRL)
                    self.PP.kf.setImage(with: profileImageuRL as? Resource)

                }
                if let coverImageuRL = dict["CoverPic"] as? String
                {
                    self.coverPic.kf.setImage(with: coverImageuRL as? Resource)
                }
            }
        })
    }
}

}

您可以看到个人资料标签图像[https://i.stack.imgur.com/g4ENe.jpg][1] 再次谢谢你