0

I am using Xcode, Swift 3. I am trying to call putData for an image (PNG), and this is my code :

let storageRef = Storage.storage().reference().child("ProductsImages").child(product.UniqueID()).child("MainImage.png")
    if let mainChosenImage = self.selectedImageToUpload
    {
        if let uploadData = UIImagePNGRepresentation(mainChosenImage)
        {
            storageRef.putData(uploadData, metadata: nil, completion:
                {
                    (StorageMetaData, error) in
                    if error != nil
                    {
                        print(error)
                        return
                    }

                    self.mainImageURL = StorageMetaData?.downloadURL()?.absoluteString
            })
        }
    }

And the behavior is this - the image is saved fine to Firebase, but "completion" isn't called. Meaning - it won't check if error != nil, and more importantly - it won't instantiate mainImageURL with the absolute URL string.

Any ideas on how to fix this ?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
MathEnthusiast
  • 101
  • 1
  • 10

1 Answers1

1

completion should look like this

 if let uploadData = UIImagePNGRepresentation(self.myImageView.image!) {
    storageRef.put(uploadData, metadata: nil) { (metadata, error) in
        if error != nil {
            print("error")

        } else {

            // your uploaded photo url.
        }
   }
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87