I want to put the download URL of images into my Firebase Database. I can upload the Image into storage but I can't figure out how to get the URL into my database with the rest of the "post".
@IBOutlet weak var titleText: UITextField!
@IBOutlet weak var authorText: UITextField!
@IBOutlet weak var mainText: UITextView!
@IBOutlet weak var dateText: UITextField!
@IBOutlet weak var myImageView: UIImageView!
var ref:FIRDatabaseReference?
override func viewDidLoad() {
super.viewDidLoad()
ref = FIRDatabase.database().reference()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func uploadImage(_ sender: Any) {
let image = UIImagePickerController()
image.delegate = self
image.sourceType = UIImagePickerControllerSourceType.photoLibrary
image.allowsEditing = false
self.present(image, animated: true)
{
//after its completed
}
}
@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
}
print(metadata)
//how do I put the download URL in the metadata into my 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, "myImageURL": myImageURL])
//the myImageURL part is where I get an error
self.performSegue(withIdentifier: "post", sender: self)
}
else{
let alertController = UIAlertController(title: "Oops!", message: "Field left blank", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "Ok", style: .cancel, handler: nil)
alertController.addAction(defaultAction)
self.present(alertController, animated: true, completion: nil)
}
}
}
Create a new project on android studio or open an existing project in which you want to add authentication and add the firebase to that android application. Two buttons: one for selecting an image from gallery. other button is for uploading an image on firebase storage on the cloud.
The Firebase SDKs for Cloud Storage add Google security to file uploads and downloads for your Firebase apps, regardless of network quality. You can use our SDKs to store images, audio, video, or other user-generated content.
To upload a file to Cloud Storage, you first create a reference to the full path of the file, including the file name. Once you've created an appropriate reference, you then call the putBytes() , putFile() , or putStream() method to upload the file to Cloud Storage.
Upload images to Firestore js file and add the following import statements: Add a state object to keep track when an image file is picked from the gallery, as well as when there's a title provided for that image file. This is to be done inside the class component. An image can be picked using ImagePicker.
For Updated Firebase Version And Swift 4.2 Code :
func uploadMedia(completion: @escaping (_ url: String?) -> Void) {
let storageRef = Storage.storage().reference().child("\(Auth.auth().currentUser?.uid ?? "").png")
if let uploadData = self.imgUploadView.image?.jpegData(compressionQuality: 0.5) {
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)
})
// completion((metadata?.downloadURL()?.absoluteString)!))
// your uploaded photo url.
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With