我正在尝试将图片上传到Firebase并下载带有元数据的URL,我尝试了几种方法对其进行修复,但似乎无法获取。我已成功将图像上传到存储,但是无法获取该URL并将其发送到数据库。这是到目前为止的代码。有人可以帮助我了解如何获取该网址吗?
这是我的代码...
//FIREBASE CODE
@IBAction func postButtonPressed(_ sender: Any) {
if self.lost {
self.sendDataToDatabase(address: self.address.text!, breed: self.breed.text!, phone: self.phone.text!, lostfound: "lost")
}
else {
self.sendDataToDatabase(address: self.address.text!, breed: self.breed.text!, phone: self.phone.text!, lostfound: "found")
}
ProgressHUD.show("En Proceso...")
let storage = Storage.storage()
let storageRef = storage.reference()
let photoIdString = "\(NSUUID().uuidString).jpg"
let imageReference = storageRef.child("posts").child(photoIdString)
if let imageData = UIImageJPEGRepresentation(selectedImage, 0.7) {
let metaData = StorageMetadata()
metaData.contentType = "images/jpg"
_ = imageReference.putData(imageData, metadata: metaData) { (metadata, error) in
storageRef.downloadURL(completion: { (url, error) in
if error != nil {
print(error)
return
}
//Remember you still need to get the photoURL!
})
} } else {
ProgressHUD.showError("Tienes que subir una foto...")
}
}
答案 0 :(得分:0)
这是我设法解决自己的问题的方法!
@IBAction func postButtonPressed(_ sender: Any) {
let storage = Storage.storage()
let storageRef = storage.reference()
let photoIdString = "\(NSUUID().uuidString).jpg"
let imageReference = storageRef.child("posts").child(photoIdString)
if let imageData = UIImageJPEGRepresentation(selectedImage, 0.7) {
imageReference.putData(imageData).observe(.success) { (snapshot) in
imageReference.downloadURL(completion: { (url, error) in
if let downloadUrl = url {
let directoryURL : NSURL = downloadUrl as NSURL
let urlString:String = directoryURL.absoluteString!
self.sendDataToDatabase(photoUrl: urlString)
}
else {
print("couldn't get profile image url")
return
}
})}
快速笔记sendDataToDatabase是我使用setValue方法将数据发送到Firebase的函数。