[英]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.