I am trying to retrieve the profile image of the authenticated user. In the output section, I am getting the desired output like the snapshot value and the download URL link. but the Profile Image doesnt show up in the image viewer in my app.
here is my code:-
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.
}
Where Did I Possibly go Wrong ? Please help!
Edit:- i have edited my programe. Earlier it showed the error “fatal error: unexpectedly found nil while unwrapping an Optional value” but it has been solved now. thanks to the link on the comment section. it doesnt show up in the ImageViewer in my app yet.