Ad

Can't Download A Picture Url From FirebaseStorage To FirebaseDatabase - Swift - Metadata.downloadURL() Not Recognized Anymore

I'm trying to upload a picture to Firebase and download the URL with metadata, I've tried several methods to fix it but can't seem to get it. I have successfully uploaded the image to Storage, but haven't been able to get the url and send it to the Database. This is the code that I've got so far. Could anybody help me understand how I could get the url?

Here's my code...

    //FIREBASE CODE

    @IBAction func postButtonPressed(_ sender: Any) {

        if self.lost {
            self.sendDataToDatabase(address: self.address.text!, breed: self.breed.text!, phone: self.phone.text!, lostfound: "lost")
        }

        else {
            self.sendDataToDatabase(address: self.address.text!, breed: self.breed.text!, phone: self.phone.text!, lostfound: "found")
        }

        ProgressHUD.show("En Proceso...")
        let storage = Storage.storage()
        let storageRef = storage.reference()
        let photoIdString = "\(NSUUID().uuidString).jpg"
        let imageReference = storageRef.child("posts").child(photoIdString)
        if let imageData = UIImageJPEGRepresentation(selectedImage, 0.7) {
            let metaData = StorageMetadata()
            metaData.contentType = "images/jpg"
            _ = imageReference.putData(imageData, metadata: metaData) { (metadata, error) in
             storageRef.downloadURL(completion: { (url, error) in

                if error != nil {
                    print(error)
                    return
                }
                //Remember you still need to get the photoURL!

            })

            } } else {
            ProgressHUD.showError("Tienes que subir una foto...")
        }
                                                    }
Ad

Answer

This is the way I managed to solve my own question!

 @IBAction func postButtonPressed(_ sender: Any) {

  let storage = Storage.storage()
    let storageRef = storage.reference()
    let photoIdString = "\(NSUUID().uuidString).jpg"
    let imageReference = storageRef.child("posts").child(photoIdString)
    if let imageData = UIImageJPEGRepresentation(selectedImage, 0.7) {
        imageReference.putData(imageData).observe(.success) { (snapshot) in
        imageReference.downloadURL(completion: { (url, error) in

            if let downloadUrl = url {

                let directoryURL : NSURL = downloadUrl as NSURL
                let urlString:String = directoryURL.absoluteString!
               self.sendDataToDatabase(photoUrl: urlString)
            }
                else {
                print("couldn't get profile image url")
                return
            }
            })}

Quick note sendDataToDatabase is a function I have where I use the my setValue method to send the data to Firebase.

Ad
source: stackoverflow.com
Ad