gpt4 book ai didi

swift - 存储中的 Firebase 图像被重写,仅保留最后两个

转载 作者:行者123 更新时间:2023-11-30 10:27:21 24 4
gpt4 key购买 nike

在我的 Firebase 项目中,用户可以上传图像,并将其存储在 Firebase 存储上名为 images 的文件夹中。

但是,每当我通过上传图像来测试它时,该文件夹仅显示我最近上传的 2 张图像。我希望它保留所有上传的图像。

有其他人遇到过类似的问题吗?我不知道问题出在我的代码中还是 Firebase 中,但这是我的代码:

import UIKit
import FirebaseStorage

class PostViewController: UIViewController,UINavigationControllerDelegate, UIImagePickerControllerDelegate {

@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var noteTextField: UITextField!
var imagePicker = UIImagePickerController()
var imageName = "\(NSUUID().uuidString).jpeg"
var imageURL = ""


override func viewDidLoad() {
super.viewDidLoad()
let tapGesture = UITapGestureRecognizer(target: view, action: #selector(UIView.endEditing))
view.addGestureRecognizer(tapGesture)
imagePicker.delegate = self

}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let selectedImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
imageView.image = selectedImage

}
dismiss(animated:true, completion: nil)
}




@IBAction func cameraTapped(_ sender: Any) {
imagePicker.sourceType = .camera
present(imagePicker,animated: true, completion: nil)
}

@IBAction func choosePhotoTapped(_ sender: Any) {
imagePicker.sourceType = .photoLibrary
present(imagePicker,animated: true, completion: nil)
}
@IBAction func postTapped(_ sender: Any) {

let imagesFolder = Storage.storage().reference().child("images")
if let image = imageView.image{
if let imageData : Data = image.jpegData(compressionQuality: 1){
imagesFolder.child(imageName).putData(imageData, metadata: nil) { (metaData, error) in
if let error = error{
print(error)

}else{
imagesFolder.child(self.imageName).downloadURL { (url, error) in
if let imageURL = url?.absoluteString{
self.imageURL = imageURL
self.performSegue(withIdentifier: "successSegue", sender: nil)
}
}
}
}
}
}
}

}

Firebase 设置中可能存在此问题吗?

最佳答案

一个问题是,当实例化 PostViewController 时,会填充 imageName var,并且每次点击保存图像时,都会以相同的名称保存。例如,如果应用程序重新启动,因为将实例化新的 PostViewController,这将创建新的 imageName 属性,则名称将会更改

修复方法是移动 imageName 属性,以便每次用户点击时都会创建它

    class PostViewController: UIViewController,UINavigationControllerDelegate, UIImagePickerControllerDelegate {

@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var noteTextField: UITextField!
var imagePicker = UIImagePickerController()
|--- //var imageName = "\(NSUUID().uuidString).jpeg" //move this
| var imageURL = ""
| .
| .
| @IBAction func postTapped(_ sender: Any) {
|
| let imagesFolder = Storage.storage().reference().child("images")
| if let image = imageView.image {
| if let imageData : Data = image.jpegData(compressionQuality: 1) {
|----> let imageName = "\(NSUUID().uuidString).jpeg" //to here
imagesFolder.child(imageName).putData(imageData, metadata: nil) { (metaData, error) in
if let error = error{

关于swift - 存储中的 Firebase 图像被重写,仅保留最后两个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59881716/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com