[Solved] Upload an image from device to firebase


Well, I don’t know whats going on but, this works for me,

Make sure your pods has at least these in there.

PODFILE

pod 'Firebase/Storage'
pod 'Firebase/Auth' #Auth isn't needed but, you should really use it.

View controller

import UIKit
import FirebaseStorage

class TestView: UIViewController {
    var imageView: UIImageView!


    override func viewDidLoad() {
        super.viewDidLoad()
        imageView = UIImageView()

    }

    @IBAction func uploadPicture(_ sender: Any) {
        self.imageView.image = #imageLiteral(resourceName: "playlist.png")

        uploadMedia() { url in
             guard let url = url else { return }
            print(url)
        }

    }

    func uploadMedia(completion: @escaping (_ url: String?) -> Void) {

       let storageRef = Storage.storage().reference().child("something.png")
       if let uploadData = self.imageView.image?.pngData(){ //You can change this to jpeg, etc
           storageRef.putData(uploadData, metadata: nil) { (metadata, error) in
               if error != nil {
                   print("error")
                   completion(nil)
               } else {

                   storageRef.downloadURL(completion: { (url, error) in

                       print(url?.absoluteString)
                       completion(url?.absoluteString)
                   })

               }
        }}}

}

0

solved Upload an image from device to firebase