0

I would like to know how I can fetch an image back that I have stored to firebase storage so that I can display it as a user profile image?

private let storage = Storage.storage().reference()
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    if let image = info[.originalImage] as? UIImage, let imageData = image.pngData() {
        imageView.image = image
        let storageRef = Storage.storage().reference().child("ProfileImages").child("profile.png")
        let metaData = StorageMetadata()
        metaData.contentType = "image/png"
        storageRef.putData(imageData, metadata: metaData) { (metaData, error) in
            if error == nil, metaData != nil {
                storageRef.downloadURL { url, error in
                    if let url = url {
                        print(url)//URL of the profile image
                        self.saveProfileImageUrlInUserDetails(url: url)
                    }
                }
            } else {
                print(error?.localizedDescription as Any)//upload failed
            }
        }
    }
    picker.dismiss(animated: true, completion: nil)
}

func saveProfileImageUrlInUserDetails(url: URL) {
    let changeRequest = Auth.auth().currentUser?.createProfileChangeRequest()
    changeRequest?.photoURL = url
    changeRequest?.commitChanges(completion: { error in
        if error == nil {
            //saved
        } else {
            print(error?.localizedDescription as Any)//failed to udpate
        }
        
    })
}
JideGuru
  • 7,102
  • 6
  • 26
  • 48
Zack
  • 1
  • 3
  • Use the `getData` method. – El Tomato Jun 27 '20 at 12:22
  • Could you show me how please I'm not sure what to write? – Zack Jun 27 '20 at 12:25
  • After uploading the image upload the download url to firebase realtime database, under user's node write condition if userId=>child('profileImage')=not null, fetch the download url and set your profile image using the url. So every time if u open that specific activity it will first check in the database if the user uploaded his.her profile image and if yes fetch the download url and set it to image view – 50_Seconds _Of_Coding Jun 27 '20 at 13:13
  • Since you already have a download URL (which is a regular URL to the image, that doens't require any Firebase-specific treatment), you can just display it as you would wth any other URL. So https://www.google.com/search?q=site:stackoverflow.com+swift+show+image+from+url – Frank van Puffelen Jun 27 '20 at 13:43

0 Answers0