The wrong image is being uploaded to my Firebase Storage

124 Views Asked by At

Everything works perfectly. I see an image being uploaded to my Firebase storage, the only problem is, it's the image that I placed as the imageview, and not the image that is being selected from the camera roll.

import UIKit
import Firebase

class ProfileViewController: UIViewController,     UIImagePickerControllerDelegate, UINavigationControllerDelegate {

@IBOutlet weak var profilePicture: UIImageView!

@IBAction func dismissButton(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}

override func viewDidLoad() {
    super.viewDidLoad()
    profilePicture.translatesAutoresizingMaskIntoConstraints = false
    profilePicture.addGestureRecognizer(UITapGestureRecognizer(target:self, action: #selector(handleSelectProfileImageView)))
    guard let uid = FIRAuth.auth()?.currentUser?.uid else {
        return
    }

    let imageName = NSUUID().uuidString
    let storageRef =   FIRStorage.storage().reference().child("Profile_Images").child("\(imageName).jpg")

    if let uploadData = UIImageJPEGRepresentation(self.profilePicture.image!, 0.1){
    storageRef.put(uploadData, metadata: nil, completion: { (metadata, error) in

    if error != nil{
        print(error as Any)
        return
    }
        if let profileImageURL = metadata?.downloadURL()?.absoluteString {
         let values = ["profileImageUrl": profileImageURL];

        self.registerUserIntoDatabaseWithUID(uid: uid, values: values as [String : AnyObject])
        }
    })
}
}
   private func registerUserIntoDatabaseWithUID(uid: String, values:[String: AnyObject]) {
    let ref = FIRDatabase.database().reference(fromURL:"")
    let usersReference = ref.child("Users").child(uid)
    usersReference.updateChildValues(values, withCompletionBlock: { (err,ref) in

        if err != nil {
            print(err as Any)
            return
        }


    })
}


func handleSelectProfileImageView() {
   let picker = UIImagePickerController()
   picker.delegate = self
    picker.allowsEditing = true

    present(picker, animated: true, completion:nil)

    }
func imagePickerController(_ picker: UIImagePickerController,  didFinishPickingMediaWithInfo info: [String : Any]) {

    var selectedImageFromPicker:UIImage?
    if let editedImage = info["UIImagePickerControllerEditedImage"] as?  UIImage{
        selectedImageFromPicker = editedImage
    }else if let originalImage =  info["UIIMagePickerControllerOriginalImage"] as? UIImage{

        selectedImageFromPicker = originalImage

    }
    if let selectedImage = selectedImageFromPicker{
        profilePicture.image = selectedImage
    }

    dismiss(animated: true, completion: nil)

 }

func imagePickerControllerDidCancel(_ picker: UIImagePickerController)   {
    print("canceled picker")
    dismiss(animated: true, completion: nil)

        }

    }
1

There are 1 best solutions below

0
On BEST ANSWER

You are uploading the image in your imageView in the viewDidLoad. Because of this it will always upload the image you've put in the imageView as a placeholder.

See this example:

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

    var selectedImageFromPicker: UIImage?

    if let editedImage =
        info["UIImagePickerControllerEditedImage"] as? UIImage {
        selectedImageFromPicker = editedImage
    } else if let originalImage =
        info["UIImagePickerControllerOriginalImage"] as? UIImage{
        selectedImageFromPicker = originalImage
    }

    if let selectedImage = selectedImageFromPicker {
        updateAvatarImageInFirebase(image: selectedImage)
    }

    dismiss(animated: true, completion: nil)
}


func updateAvatarImageInFirebase(image: UIImage){

    let user = FIRAuth.auth()?.currentUser

    guard let uid = user?.uid else {
        return
    }

    if user != nil {
        let storageRef = FIRStorage.storage().reference().child("users").child(uid).child("profileImage.jpg")

        if let uploadData = UIImageJPEGRepresentation(image, 0.2) {
            storageRef.put(uploadData, metadata: nil, completion: { (metadata, error) in

                if error != nil {
                    print(error!)
                    return
                }

                if let profileImageUrl = metadata?.downloadURL()?.absoluteString {
                    FIRDatabase.database().reference().child("users").child(uid).updateChildValues(["profileImageUrl": profileImageUrl])
                }
            })
        }
    }
}