0

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.

  • Can't add an answer -- but you should check for an error before force unwrapping data. The error should be non-nil in that case and tell you what went wrong. – marcorei Oct 07 '17 at 09:42
  • @marcorei that error has been solved. please check my edited programe. the problem is the picture doesnt appear in the viewer in my app – Subhamoy Paul Oct 07 '17 at 09:50
  • okay well now it does work! Thanks anyway! – Subhamoy Paul Oct 07 '17 at 09:53
  • You can try this: `DispatchQueue.main.async { if let image = UIImage(data: data as! Data) as? UIImage { self.ProfileImage.image = image } } ` – 3stud1ant3 Oct 07 '17 at 10:27
  • you should always update your ui on the main thread – 3stud1ant3 Oct 07 '17 at 10:40
  • @3stud1ant3 I guess it’s not required because the code works perfectly fine and the photo does appear on the View now in the app. Still, I would give it a try and see if your code works. :) – Subhamoy Paul Oct 07 '17 at 10:53

0 Answers0