Man James
Man James

Reputation: 89

ImageView to Firebase Storage in Swift

I am trying to upload an image from ImageView to Firebase storage but it won't work.

I have listed my code below: My image view is called ProfileImage

        let storageRef = Storage.storage().reference().child("myImage.png")
        if let uploadData = self.ProfileImage.image!.pngData() {
            storageRef.putFile(from: uploadData, metadata: nil) { (metadata, error) in
                if error != nil {
                    print("error")
                    completion(nil)
                } else {
                    // your uploaded photo url.
                }
            }
        }

It comes up with the error "Cannot convert value of type 'Data' to expected argument type 'URL'

Upvotes: 0

Views: 866

Answers (4)

Captain Awesome
Captain Awesome

Reputation: 27

I know that this question has been answered, but there is an easier way to do this. Along with import Firebase and Firebase Storage, you will also have to add FirebaseUI to your podfile and import it.

After you have done that, you could get your image to your app much simpler.

let storage = Storage.storage()
let storageRef = storage.reference()
let placeholderImage = UIImage(named: "placeholder.jpeg")
let reference = storageRef.child("myImage.png")
ProfileImage.sd_setImage(with: reference, placeholderImage: placholderImage)

(The placeholder Image would just be a transparent image that you put in your assets folder in XCode that you could reuse multiple times in your application, for whenever you needed to get a Firebase Image on your app.)

Upvotes: 0

Viren Patel
Viren Patel

Reputation: 128

let refDatabase = Database.database().reference()
var refstorage = Storage.storage().reference()
let data = image.jpegData(compressionQuality: 1.0) // 
let metadata = StorageMetadata()
metadata.contentType = "image/jpeg"
let postkey = refDatabase.child("Post").childByAutoId().key
print(postkey)
let imagename = "PostImage/\(postkey).png"
refstorage = refstorage.child(imagename)
let timestamp = Date().timeIntervalSince1970 // you can use this to track time when it was uploaded. 
self.refstorage?.putData(data!, metadata: metadata, completion: { (meta, error) in

    if error == nil{
        if let imageData = meta?.downloadURL()?.absoluteString{
            // DO SOMETHING
        } else {
            print("Couldn't get the URL for image")
        }
    }
})

Upvotes: 0

Abhishek Jadhav
Abhishek Jadhav

Reputation: 706

Try this code :-

let storageRef = Storage.storage().reference().child("myImage.png")
    if let uploadData = UIImagePNGRepresentation(self.profileImage.image!) {
        storageRef.put(uploadData, metadata: nil) { (metadata, error) in
            if error != nil {
                print("\(error.localizeDescription)")
            } else {                    
                // your uploaded photo url.
            }
       }

Upvotes: 1

Galo Torres Sevilla
Galo Torres Sevilla

Reputation: 1575

You are trying to upload Data, not a file. Replace

putFile

With

putData

And it should work fine

Upvotes: 2

Related Questions