代码之家  ›  专栏  ›  技术社区  ›  kesape

Firebase存储。下载图像阵列

  •  0
  • kesape  · 技术社区  · 3 年前

    我正在尝试从Firebase存储文件夹下载一些图像,并将其添加到我的模型中 Rules Firebase Folder Images in folder

    我的模型在这里。我必须使用文件夹中具有相同字符串名称的图像,而不是字符串(1、2、3、4、5)

    struct ImageModel {
        var images: String
        var index: Int
        var imageLabel: String
        var description: String
        var comment = [String]()
    }
    
    class Model {
       static var imageModel = [
            ImageModel(images: "1", index: 0, imageLabel: "MDA", description: "description", comment: []),
            ImageModel(images: "2", index: 1, imageLabel: "Picture!", description: "some description.", comment: []),
            ImageModel(images: "3", index: 2, imageLabel: "What is this??", description: "description", comment: []),
            ImageModel(images: "4", index: 3, imageLabel: "A long named picture", description: "description", comment: []),
        ]
    }
    

    我找到了写请求的方法

    func uploadMedia() {
            let storageRef = Storage.storage().reference().child("pictures")
            let megaByte = Int64(1 * 1024 * 1024)
            
            storageRef.getData(maxSize: megaByte) { (data, error) in
                guard let data = data else { return }
                let image = UIImage(data: data)
                for i in Model.imageModel {
                    i.images = image
                }
            }
        }
    

    但它有一些错误,比如

    无法分配给属性:i是let常量。。。和不能 是否为UIImage类型赋值?键入字符串的步骤

    如何更改型号或请求?

    我在中使用我的模型 CollectionViewCell . 所以我有collectionView和Cell在里面。单元格具有图像和名称。这张照片是我必须从中获得的 Firebase Storage 我的模型中已经有了这个名字。原因是我使用的存储有时会更改图像

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            guard let cell = self.collectionView.dequeueReusableCell(withReuseIdentifier: CollectionViewCell.identifier, for: indexPath) as? CollectionViewCell else { return UICollectionViewCell() }
            cell.configureCollectionViewCell(picture: UIImage(named: String(Model.imageModel[indexPath.row].images))!, imageLabel: Model.imageModel[indexPath.row].imageLabel)
            return cell
        }
    

    UPD:

    我将模型更改为:

    struct ImageModel {
        var images: UIImage
        var index: Int
        var imageLabel: String
        var description: String
        var comment = [String]()
    }
    
    class Model {
       static var imageModel = [
        ImageModel(images: UIImage(), index: 0, imageLabel: "MDA", description: "description", comment: []),
        ImageModel(images: UIImage(), index: 1, imageLabel: "Picture!", description: "some description.", comment: []),
        ImageModel(images: UIImage(), index: 2, imageLabel: "What is this??", description: "description", comment: []),
        ImageModel(images: UIImage(), index: 3, imageLabel: "A long named picture", description: "description", comment: []),
        ]
    }
    

    然后我把方法 ViewWillAppear 并试图得到错误或结果

    override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
            self.uploadMedia()
        }
        
        func uploadMedia() {
            let storageRef = Storage.storage().reference().child("pictures")
            let megaByte = Int64(1 * 1024 * 1024)
            
            storageRef.getData(maxSize: megaByte) { (data, error) in
                guard let data = data else { return }
                let image = UIImage(data: data)
                for (index, i) in Model.imageModel.enumerated() {
                    Model.imageModel[index].images = image!
                }
                guard let error = error else  { return }
                print(error.localizedDescription)
            }
        }
    

    什么都没发生,日志是空的

    2022-05-29 20:20:09.668785+0700 diplom[14378:13214993][boringssl] boringssl\u metrics\u log\u metric\u block\u invoke(153)未能记录指标

    我还有一个方法可以帮助我将模型保存到另一个模型。这就是为什么我想在我的模型中使用字符串

    给你。。 CollectionView 准备 Detail VC

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
            let vc = DetailVC()
            vc.picture = UIImageView(image: Model.imageModel[indexPath.row].images)
            vc.imageLbel.text = Model.imageModel[indexPath.row].imageLabel
            vc.pictureNamed = Model.imageModel[indexPath.row].images
            vc.index = Model.imageModel[indexPath.row].index
            vc.descripLabel.text = Model.imageModel[indexPath.row].description
            self.navigationController?.pushViewController(vc, animated: true)
        }
    

    详图VC 所有物

     var pictureNamed = String()
    

    保存的收集单元模型

    struct savedCategory: Codable {
        var images: String
        var index: Int
        var imageLabel: String
        var description: String
        var comment = [String]()
    }
    
    
    class SavedCategoryModel {
        static var categoryModel: [savedCategory] = []
    }
    

    UserDefaults 我以前使用codable模型的地方

    class UserDefaultsConfig {
        func saveData() {
            let defaults = UserDefaults.standard
            defaults.set(try? PropertyListEncoder().encode(SavedCategoryModel.categoryModel), forKey:"SavedStringArray")
        }
        func showList() {
            let defaults = UserDefaults.standard
            if let data = defaults.value(forKey:"SavedStringArray") as? Data { SavedCategoryModel.categoryModel = try! PropertyListDecoder().decode(Array<savedCategory>.self, from: data) }
        }
    }
    

    这是我的方法。所以有很多变化。。。是否有分辨率来获取图像字符串?

    @objc func savePicture() {
            let newArray = savedCategory(images: self.pictureNamed, index: index, imageLabel: self.imageLbel.text!, description: self.descripLabel.text ?? "empty", comment: Model.imageModel[index].comment)
            SavedCategoryModel.categoryModel.append(newArray)
            self.userDefaults.saveData()
            self.userDefaults.showList()
        }
    
    
    1 回复  |  直到 3 年前
        1
  •  0
  •   Heron. F    3 年前

    您希望直接访问图像数组,而不是对每个项目的引用。

    for (index, i) in Model.imageModel.enumerated() {
        Model.imageModel[index].images = image
    }
    

    此外,您的 images 属性接受字符串,定义如下:

    var images: String
    

    但你想给它分配一个 UIImage . 我不知道你想给 ImageModel ,也许你可以详细说明一下?

    对更新问题的答复:

    首先,允许结构接受 图像 作为一个 UIImage公司 :

    struct ImageModel {
        var images: UIImage // <-- from String to UIImage
        var index: Int
        var imageLabel: String
        var description: String
        var comment = [String]()
    }
    

    然后,更改此行:

    cell.configureCollectionViewCell(picture: UIImage(named: String(Model.imageModel[indexPath.row].images))!, imageLabel: Model.imageModel[indexPath.row].imageLabel)
    

    收件人:

    cell.configureCollectionViewCell(picture: Model.imageModel[indexPath.row].images, imageLabel: Model.imageModel[indexPath.row].imageLabel)
    

    最后一个建议是,您似乎只在其中存储了一张图像 ImageModel.images ,所以可以将变量名称更改为单数 image 像这样:

    struct ImageModel {
        var image: UIImage // <-- from String to UIImage
        var index: Int
        var imageLabel: String
        var description: String
        var comment = [String]()
    }
    

    让我知道这有道理