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

问题描述 投票:0回答:1

在我的Firebase项目中,用户可以上传图像,并且该图像存储在Firebase Storage上名为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的设置中存在吗?

swift firebase firebase-storage
1个回答
0
投票

一个问题是,在实例化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{
© www.soinside.com 2019 - 2024. All rights reserved.