在firebase存储中添加不同的图像
我想在我的 firebase Storage 中添加不同的图像,但问题是,我的代码只在存储文件中添加一张图像,是否可以添加多个图像?
let userID = Auth.auth().currentUser!.uid
let db = Firestore.firestore()
guard let imageData = self.countryImage.image?.jpegData(compressionQuality: 0.4) else {
return
}
let storageRef = Storage.storage().reference(forURL: "gs://covidregulationsapp.appspot.com/")
let storageProfileRef = storageRef.child("savedImage").child(userID)
let metadata = StorageMetadata()
metadata.contentType = "image/jpeg"
storageProfileRef.putData(imageData,metadata: metadata)
{ (storageMetadata, error) in
if error != nil {
print("error")
}
storageProfileRef.downloadURL(completion: {(url,error) in
let db = Firestore.firestore()
db.collection("users/\(userID)/saved")
.getDocuments() { (querySnapshot, err) in
if let err = err {
print("Couldnt update : \(err)")
} else {
db.collection("users/\(userID)/saved").addDocument(data: [
"country":self.countryTitle.text!,
"code":self.info,
"city":self.airport.text!,
"countryImage": url?.absoluteString
])
}
}
})
}
如上图所示,我的 savedImage
存储中只有一张图像,它会更改 URL 如果我尝试添加不同的图像,我的最终结果是在 savedImage
存储中添加多个图像
I want to add different images in my firebase Storage, but the problem is that, my code only adds one in a Storage file, is it possible to add more than one?
let userID = Auth.auth().currentUser!.uid
let db = Firestore.firestore()
guard let imageData = self.countryImage.image?.jpegData(compressionQuality: 0.4) else {
return
}
let storageRef = Storage.storage().reference(forURL: "gs://covidregulationsapp.appspot.com/")
let storageProfileRef = storageRef.child("savedImage").child(userID)
let metadata = StorageMetadata()
metadata.contentType = "image/jpeg"
storageProfileRef.putData(imageData,metadata: metadata)
{ (storageMetadata, error) in
if error != nil {
print("error")
}
storageProfileRef.downloadURL(completion: {(url,error) in
let db = Firestore.firestore()
db.collection("users/\(userID)/saved")
.getDocuments() { (querySnapshot, err) in
if let err = err {
print("Couldnt update : \(err)")
} else {
db.collection("users/\(userID)/saved").addDocument(data: [
"country":self.countryTitle.text!,
"code":self.info,
"city":self.airport.text!,
"countryImage": url?.absoluteString
])
}
}
})
}
as you can see in the picture above, there is only one image in my savedImage
Storage, it changes URL If I try to add different image, my end result is to add several images in the savedImage
Storage
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将每个图像上传到此参考:
因此,当您上传第二个图像时,您最终会覆盖来自同一用户的第一个图像。
如果要为某个用户上传多张图片,常见的做法是使用用户ID作为“目录”名称,然后在该目录下生成唯一的图片名称。
像这样的事情:
You upload each image to this reference:
So when you upload a second image, you end up overwriting the first one from that same user.
If you want to upload multiple images for a user, a common approach is to use the user ID as the "directory" name, and then generate a unique image name under there.
Something like this: