Failed to load image from Firebase database swift3

Viewed 97

I'm trying to load data from the firebase. I successfully load the data like usename and email, but somehow it fails to load the image. I'm attaching my code which I have used to load the data from firebase. Please help. thank you.

code :

import UIKit
import FirebaseDatabase
import  Firebase

class ProfileVC: UIViewController {


@IBOutlet weak var currentphoto: UIImageView!

@IBOutlet weak var usernameLabel: UILabel!

@IBOutlet weak var BioOrEmailLabel: UILabel!

var databasereff : DatabaseReference!


override func viewDidLoad() {
    super.viewDidLoad()

    databasereff = Database.database().reference()
    if let userid = Auth.auth().currentUser?.uid
    {
        databasereff.child("users").child(userid).observeSingleEvent(of: .value, with: { (snapshot) in
            let dict = snapshot.value as? [String:Any]
            let username = dict?["username"] as? String
            let email = dict?["email"] as? String
            if let photourl = dict?["profileimageUrl"] as? String
            {
                let url = URL(string: photourl)
                URLSession.shared.dataTask(with: url!, completionHandler: { (data, response, error) in
                    if error != nil{
                            print(error?.localizedDescription)
                        return
                    }
                    OperationQueue.main.addOperation  {
                        self.currentphoto.image = UIImage(data: data!)
                    }
                }).resume()
            }
            self.usernameLabel.text = username
            self.BioOrEmailLabel.text = email
        })
        {
            (error) in
            print(error.localizedDescription)
        }
    }

    // Do any additional setup after loading the view.
}

 }
1 Answers
Related