我正在尝试检索经过身份验证的用户的个人资料图片。 在输出部分,我得到了所需的输出,如快照值和下载URL链接。但是我的应用程序中的图像查看器中没有显示配置文件图像。
这是我的代码: -
import UIKit
import FirebaseAuth
import FirebaseDatabase
import FirebaseStorage
class TempViewController: UIViewController {
@IBOutlet weak var myLabel: UILabel!
@IBOutlet weak var ProfileImage: UIImageView!
@IBAction func Logout(_ sender: Any) {
try! Auth.auth().signOut()
performSegue(withIdentifier: "logout", sender: self)
}
override func viewDidLoad() {
super.viewDidLoad()
loggedinuser()
print(Auth.auth().currentUser?.email)
print(Auth.auth().currentUser?.uid)
}
func loggedinuser() {
if Auth.auth().currentUser?.uid != nil
{
let uid = Auth.auth().currentUser?.uid
Database.database().reference().child("User").child(uid!).observeSingleEvent(of: .value, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject?]
{
self.myLabel.text = dictionary["username"] as? String
print (snapshot)
let downloadURL = dictionary["profileImageURL"] as? String
print (downloadURL)
self.loadingImageFromDatabase(downloadURL: downloadURL!)
}
})
}
}
func loadingImageFromDatabase(downloadURL: String) {
let storageRef = Storage.storage().reference(forURL: downloadURL)
storageRef.downloadURL { (url, error) in
let data = try? Data(contentsOf: url!)
let image = UIImage(data: data as! Data)
self.ProfileImage.image = image
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
我可能在哪里错了?请帮忙!
编辑: - 我编辑了我的程序。之前它显示了错误 “致命错误:在解开一个Optional值时意外地发现了nil”,但现在已经解决了。感谢评论部分的链接。它还没有显示在我的应用程序中的ImageViewer中。