简体   繁体   中英

ios uploading image to Firebase but it's not when I am trying to download it?

I am trying to upload my userProfile image onto Firebase however, though the upload was successful but when I downloaded it via the download link on the firebase console, it's downloaded as a Document file and not the jpg file that I've uploaded? Also, my UIImage took over all my views in the view controller when I set the image to a specific image.

here's my code:

@IBAction func registerRegisterButton(_ sender: UIButton) {
    let userProfileImageRef = self.storage.reference().child("userProfileImage")

    //testing image setup
    self.registerUserProfileImage.image = UIImage(named:"puppy.jpg")

    // Registering user with email
    Auth.auth().createUser(withEmail: registerUserEmailTextField.text!, password: registerUserPasswordTextField.text!) { (user, error) in

        if error != nil {
            print(error)
        } else {
            print("Registration complete")

            // if register succcessful, we upload the user profile image to firebase
            let data = Data()

            //uploading user profile picture
            if let uploadData = UIImagePNGRepresentation(self.registerUserProfileImage.image!) {
                userProfileImageRef.putData(uploadData, metadata: nil, completion: { (metadata, error) in

                    if error != nil {
                        print(error)
                        return
                    } else {
                        print("Success upload!")
                        self.dismiss(animated: true, completion: nil)
                        self.performSegue(withIdentifier: "registrationCompleteSegue", sender: self)
                    }
                })

            }
        }
    }
}

Thank you for your help in advance

I think you're trying to download the URL of Image and setting it into ImageView. If yes, then create a meta data of type StorageMetadata and set the content type as image/jpeg . Then pass it in putData method of Firebase Reference and get the ImageURL with the metadata parameter of putData method.

let metaData = StorageMetadata()
metaData.contentType = "image/jpg"

        if let uploadData = UIImagePNGRepresentation(self.registerUserProfileImage.image!, 0.8) as NSData {

           userProfileImageRef.putData(uploadData as Data, metadata: metaData, completion: { (metadata, error) in

                if error != nil {
                    print(error)
                    return
                } else {
                    print("Success upload!")
                    var imageURL = (metadata?.downloadURLs?[0].absoluteString)!  //Here's your image url. Then just set it to ImageView

                    self.dismiss(animated: true, completion: nil)
                    self.performSegue(withIdentifier: "registrationCompleteSegue", sender: self)
                }
           })
       }

For setting Image with URL. Kindly check this link https://stackoverflow.com/a/43486949/4608334

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM