0

I had this in the viewDidLoad of my ProfileViewController

 self.user = Auth.auth().currentUser
      self.databaseRef.child("user_profiles").child(self.user!.uid).observeSingleEvent(of: .value) { (snapshot:DataSnapshot) in

          let snapshotValue = snapshot.value as? NSDictionary


        if(snapshotValue?["about"] != nil)
        {

        }

        if(snapshotValue?["profile_pic"] != nil)
        {
            let databaseProfilePic = snapshotValue!["profile_pic"]
                as! String

            let data = try? Data(contentsOf: URL(string: databaseProfilePic)!)

            self.setProfilePicture(imageView: self.profilePic,imageToSet: UIImage (data:(data!))!)
        }

with also this function

 internal func setProfilePicture(imageView:UIImageView,imageToSet:UIImage)
    {

        imageView.layer.cornerRadius = 22.5
        imageView.layer.borderColor = UIColor.white.cgColor
        imageView.layer.masksToBounds = true
        imageView.image = imageToSet

    }

to load the profile picture of the authenticated users. My only problem is that every time i go into my ProfileViewController it takes 3-4 seconds to load the picture while i would like that as on instagram, whatsapp etc. the image is already loaded, so there is no such annoying waiting time. How can i do?

arc4Random
  • 101
  • 8

1 Answers1

1

for fast download image async you can use this library

and you can simply download image with less line of code

if(snapshotValue?["profile_pic"] as? String != nil) {
   if let imgUrl = URL(string:snapshotValue?["profile_pic"] as! String) 
   {
       self.profilePic.kf.setImage(with: imgUrl)
       self.profilePic.layer.cornerRadius = min(self.profilePic.frame.height,self.profilePic.frame.width) / 2 //you can change the cornerRadius according to your need
       self.profilePic.layer.borderColor = UIColor.white.cgColor
       self.profilePic.layer.masksToBounds = true
   }
}

Hope this will help you

Ganesh Manickam
  • 1,782
  • 3
  • 17
  • 26