0

I am trying to take a printed item and inserting it into my Firebase Database along with the rest of outlets. Thanks!

@objc(imagePickerController:didFinishPickingMediaWithInfo:) func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
{
    if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
        myImageView.image = image  
    } else {
        //error
    }

    self.dismiss(animated: true, completion: nil)

    let storageRef = FIRStorage.storage().reference().child("myImage.png")
    if let uploadData = UIImagePNGRepresentation(self.myImageView.image!) {
        storageRef.put(uploadData, metadata: nil, completion:
        {
                (metadata, error) in
                if error != nil {
                    print("error")
                    return
                } else {
                    print((metadata?.downloadURL()?.absoluteString)!)
                   //i want to take the line above and insert it into the database
                }
        })
     }           
}

@IBAction func addPost(_ sender: Any) {

    if self.titleText.text != "" && self.authorText.text != "" && self.mainText.text != "" && self.dateText.text != "" {
        ref?.child("Posts").childByAutoId()
                           .setValue(["Title": titleText.text,
                                      "Article": mainText.text,
                                      "Author": authorText.text,
                                      "Date": dateText.text ])

        //insert the download URL above 
        self.performSegue(withIdentifier: "kost", sender: self)    
    }
}
Jay
  • 34,438
  • 18
  • 52
  • 81
Riccardo
  • 289
  • 1
  • 4
  • 22
  • Did my answer help, maybe some further explanation of your question would be helpful – joshLor May 24 '17 at 03:27
  • Answered for your question here. https://stackoverflow.com/a/44061967/5878081 – Vlad Pulichev May 24 '17 at 06:08
  • Possible duplicate of [Uploading Image to Firebase Storage and Database](https://stackoverflow.com/questions/44060518/uploading-image-to-firebase-storage-and-database) – Vlad Pulichev May 24 '17 at 07:02
  • Can you explain specifically what isn't working? There may be some coding errors; *storageRef.put(uploadData, metadata: nil, completion:* should probably be *storageRef.putData(uploadData, metadata: nil) { (metadata, error) in*. See the [Firebase Upload Files Guide](https://firebase.google.com/docs/storage/ios/upload-files) for details. – Jay May 24 '17 at 17:25

1 Answers1

0

I see what your problem is now. You are tackling uploading images to firebase in totally the wrong manner. In the event didFinishPickingMediaWithInfo you should not be doing anything with firebase. You should only be setting your UIImageViews image to store the value of the data given from the image picker.

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    self.dismiss(animated: true, completion: nil)
    if let editedImage = info[UIImagePickerControllerEditedImage] as? UIImage{
        myImageView.image = editedImage
    }else{
        print("Something went wrong")
    }
}

Then in your IBAction, you should upload everything to firebase. Here is a simple function that will upload your image to the database it:

func uploadImage(){
    if let fileData = UIImageJPEGRepresentation(myImageView.image!, 0.8){
    let storageRef = storage.reference().child("images").child("testImage.jpg")
        storageRef.put(fileData, metadata: nil, completion: { (metadata, error) in
            if error != nil{
                print(error?.localizedDescription ?? "error")
                return
            }
            let downloadURL = metadata?.downloadURL()?.absoluteString
            // Write the download URL to your Database
            self.ref?.child("images").setValue(downloadURL)
        })
    }else{
        print("error")
    }
}
joshLor
  • 1,034
  • 1
  • 12
  • 27
  • This statement *it is difficult to set variables inside a firebase closure* is totally inaccurate. Class vars can be easily set within a closure; self.var_name or self.myArray.append(snapshot) works every time. – Jay May 24 '17 at 17:14