在firebase存储中添加不同的图像

发布于 2025-01-09 13:47:21 字数 1561 浏览 1 评论 0原文

我想在我的 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                        
                    ])
                }
            }
    })
}

firebaseStorage

如上图所示,我的 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                        
                    ])
                }
            }
    })
}

firebaseStorage

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

过潦 2025-01-16 13:47:21

您将每个图像上传到此参考:

let storageProfileRef = storageRef.child("savedImage").child(userID)

因此,当您上传第二个图像时,您最终会覆盖来自同一用户的第一个图像。

如果要为某个用户上传多张图片,常见的做法是使用用户ID作为“目录”名称,然后在该目录下生成唯一的图片名称。

像这样的事情:

let uuid = UUID().uuidString
let storageProfileRef = storageRef.child("savedImage").child(userID).child(uuid)

You upload each image to this reference:

let storageProfileRef = storageRef.child("savedImage").child(userID)

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:

let uuid = UUID().uuidString
let storageProfileRef = storageRef.child("savedImage").child(userID).child(uuid)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文