Ad

Upload Image To Firebase From UIImagePicker

I would like to upload a profile picture to Firebase when my UIImage picker has been chosen. But I don't know how to do it and I can not understand the documentation, because I'm new in coding). Please, help me. I need to upload picture to Firebase. It will be user picture, so reference to picture should save to Database.

extension ProfileViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {




    let image = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
    profileImage.image = image
   picker.dismiss(animated: true, completion: nil)
}

I don't how to upload and dowload profile image.

Ad

Answer

Create a storage reference and add folder name and image names.

Storage.storage().reference().child("id").child("profile.png")

and use putData:metadata:completion: method to upload the image. Once image has been uploaded get the image url and update it in user details.

Upload profile image

import FirebaseStorage
import FirebaseAuth

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    if let image = info[.originalImage] as? UIImage, let imageData = image.pngData() {
        profileImage.image = image
        let storageRef = Storage.storage().reference().child("id").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)//upload failed
            }
        }
    }
    picker.dismiss(animated: true, completion: nil)
}

Save uploaded photo reference in user detailUpdate the user's profile

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)//failed to udpate
        }
    })
}

Download profile image

if let profileImgUrl = Auth.auth().currentUser?.photoURL?.absoluteString {
    let ref = Storage.storage().reference(forURL: profileImgUrl)
    ref.getData(maxSize: 1 * 1024 * 1024) { data, error in
        if let error = error {
            print(error.localizedDescription)
        } else if let data = data, let image = UIImage(data: data) {
                self.profileImage.image = image
        }
    }
}
Ad
source: stackoverflow.com
Ad