gpt4 book ai didi

swift - 无法将图片网址从 FirebaseStorage 下载到 FirebaseDatabase - Swift - 不再识别metadata.downloadURL()

转载 作者:行者123 更新时间:2023-11-30 11:19:18 24 4
gpt4 key购买 nike

我正在尝试将图片上传到 Firebase 并下载包含元数据的网址,我尝试了多种方法来修复它,但似乎无法获取它。我已成功将图像上传到存储,但无法获取 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...")
}
}

最佳答案

这就是我解决自己问题的方法!

 @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 的函数。

关于swift - 无法将图片网址从 FirebaseStorage 下载到 FirebaseDatabase - Swift - 不再识别metadata.downloadURL(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51458804/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com