Como armazenar imagens no armazenamento firebase e salvar o link para elas no banco de dados?
Procurei uma resposta para isso. O mais próximo que cheguei éaqui, no entanto, ele não responde exatamente à minha pergunta. Sendo assim, como armazenar uma referência às imagens salvas no armazenamento do firebase, no banco de dados.
Abaixo está o código que eu tentei. É capaz de armazenar uma imagem quando carregada, mas não tenho certeza se é isso que eles significam ao armazenar a referência.
if let imageData = UIImageJPEGRepresentation(image, 0.8) {
let metadata = storageRef //.child("poop/")
let uploadTask = metadata.putData(imageData, metadata: nil) {
(metadata, error) in
guard let metadata = metadata else {
// Uh-oh, an error occurred!
return
}
// You can also access to download URL after upload.
storageRef.downloadURL {
(url, error) in
guard let downloadURL = url else {
// Uh-oh, an error occurred!
return
}
//let imgURL = url
//database integration
let ref = Database.database().reference()
let usersRef = ref.child("usersPosts")
let uid = Auth.auth().currentUser?.uid
let newUserRef = usersRef.child(uid!)
//creates a child for email and password (i think we shud store password so we can tell sumone what it is inmediatly, maybe)
newUserRef.setValue(["Image": "\(downloadURL)"])
}
}
// let imgURL = storageRef.downloadURL
//
// //database integration
// let ref = Database.database().reference()
// let usersRef = ref.child("usersPosts")
//
// let uid = Auth.auth().currentUser?.uid
// let newUserRef = usersRef.child(uid!)
// //creates a child for email and password (i think we shud store password so we can tell sumone what it is inmediatly, maybe)
//// newUserRef.setValue(["Image": "\(imgURL)"])
// For progress
uploadTask.observe(.progress, handler: { (snapshot) in
guard let progress = snapshot.progress else {
return
}
let percentage = (Float(progress.completedUnitCount) / Float(progress.totalUnitCount))
progressBlock(Double(percentage))
})
} else {
completionBlock(nil, "Image could not be converted to Data.")
}
Agradeço a ajuda!