简体   繁体   中英

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. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM