简体   繁体   English

每次用户在 firebase 存储中拍摄新照片时创建一个新图像

[英]Create a new image every time user takes a new photo in firebase storage

In my app user presses the button to take the photo and then it uploads to firebase storage but every time I do a new photo the old one disappears and it replaces itself with the same url string as old one had before.在我的应用程序中,用户按下按钮拍摄照片,然后将其上传到 firebase 存储空间,但每次我拍一张新照片时,旧照片都会消失,并用与旧照片相同的 url 字符串取而代之。 How can I upload a new image every time the same user takes and uploads the photo?每次同一用户拍摄并上传照片时,如何上传新图像?

Here is the code这是代码

let storage = Storage.storage().reference()
    guard  let imageData = ecoImage.jpegData(compressionQuality: 0.75) else {
        return
    }
    
    
        
    let metaData = StorageMetadata()
    metaData.contentType = "image/jpg"
    
    
    
        
        storage.child("pins/\("Eco1")").putData(imageData, metadata: metaData) { (meta, error) in
            if let err = error {
                print(err.localizedDescription)
            } else {
                storage.child("pins/\("Eco1")").downloadURL { (url, err) in
                    if let e = err {
                        print(e.localizedDescription)
                    } else {
                        let urlString = url?.absoluteString
                        doc.setData(["uid": uid, "pinLocation": ecoPin, "time": timeData, "url": urlString!]) { (error) in
                            if error != nil {
                                print(error?.localizedDescription)
                                return
                            } else {
                                print("Pin saved")
                            }
                        }
                    }
                }
            }
             print("Image sent")
        }

You need to generate the random file name for every time you put the new photo.每次放置新照片时都需要生成随机文件名。 In the code you provided, after the metadata, declare the variable with UUID.在您提供的代码中,在元数据之后,使用 UUID 声明变量。

let fname = UUID.init().uuidString.replacingOccurences(of: "-", with: "")

storage.child("pins/\(fname))...so on

This will generate the random file name each time you upload, and supposedly not replace your previous photo.这将在您每次上传时生成随机文件名,并且应该不会替换您以前的照片。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 创建新用户并在 Firebase 数据库中添加新文档并在存储中添加图像 - Create a new user and add new documents in Firebase database and add image in storage Javascript Vue 3 - 在创建新的身份验证帐户到 Firebase Auth 后无法将图像上传到 Firebase 存储 - Javascript Vue 3 - Cannot upload image to Firebase Storage after create new authentication account to Firebase Auth 无法创建新的 Microsoft 用户 Firebase Auth - Unable to create new Microsoft user Firebase Auth 如何在 Firebase 主机上自动创建新的 HTML 页面而无需每次都重新部署? - How automatically create new HTML page on Firebase Hosting without redeploy every time? Flutter Firestore 每次创建新阵列 function 调用 - Flutter Firestore create new Array every time function called 多少次 Firebase Auth 在 UserCredentail.additionalUserInfo.?isNewUser 中考虑新用户或非新用户? - how much times Firebase Auth takes to consider a user that is new or not in the UserCredentail.additionalUserInfo!.isNewUser? 在 firebase 中创建新用户的问题 - Problems with creating new user in firebase 当用户在 react js 中使用用户的 uid 创建帐户时,在 firebase 中添加一个新集合 - Add a new collection in firebase when the user create account using uid of user in react js 无法将图像第二次存储到 firebase 存储中 - Unable to Store the image for the second time in to the firebase storage 使用 Firebase 通知用户新登录 - Swift/Xcode - Notify User of New Signin with Firebase - Swift/Xcode
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM