0

How can I download the image I uploaded in Firebase?

Here is how I upload my images:

func uploadProfileImage(_ image:UIImage, completion: @escaping ((_ url:URL?)->())) {

    guard let uid = Auth.auth().currentUser?.uid else { return }

    let storage = Storage.storage()

    let storageRef = storage.reference().child("user/\(uid)")

    guard let imageData = UIImageJPEGRepresentation(image, 0.75) else { return }

    let metaData = StorageMetadata()

    metaData.contentType = "image/jpg"

    storageRef.putData(imageData, metadata: metaData) { metaData, error in

        if let error = error {
            print(error.localizedDescription)
            return
        }

        storageRef.downloadURL(completion: { (url, error) in
            if let _ = error{
                return
            }
            if url != nil{
                completion(url)
            }
        })
    }
}

func saveProfile(username:String, profileImageURL:URL, completion: @escaping ((_ success:Bool)->())) {

    guard let uid = Auth.auth().currentUser?.uid else { return }

    let databaseRef =  Database.database().reference().child("users/profile/\(uid)")

    let userObject = [

        "username": username,

        "photoURL": profileImageURL.absoluteString ] as [String:Any]

     databaseRef.setValue(userObject) { error, ref in

     completion(error == nil)
    }
}

But I'm not sure how I could download and display it. Any pointers?

Thank you in advance.

Ovidiu Dolha
  • 5,335
  • 1
  • 21
  • 30

0 Answers0