我正在尝试从Firebase存储文件夹下载一些图像,并将其存储到我的模型规则Firebase 文件夹 文件夹中的图像中
我的模特来了。而不是字符串(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
}
}
}但是它有一些错误,比如
不能转让财产:我是一个让常量.还有..。不能赋值类型UIImage?键入字符串
如何更改我的模型或请求?
我在CollectionViewCell中使用我的模型。所以我把collectionView和细胞放在里面。单元格具有图像和名称。这个图片,我必须从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 boringssl_metrics_log_metric_block_invoke(153)文凭14378:13214993 20:20:09.668785+0700未能记录指标
此外,我还有一些方法可以帮助我将模型保存到另一个模型。这就是为什么我想在我的模型中使用字符串
在这里..。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)
}Detail 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
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()
}发布于 2022-05-29 12:42:29
您希望直接访问图像数组,而不是对每个项的引用。
for (index, i) in Model.imageModel.enumerated() {
Model.imageModel[index].images = image
}另外,您的images属性接受一个字符串,如下所定义:
var images: String但是你想给它分配一个UIImage。我不知道你想给ImageModel添加什么,也许你能详细说明一下吗?
对最新问题的答复:
首先,允许您的结构接受images作为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]()
}告诉我这是有道理的
https://stackoverflow.com/questions/72423551
复制相似问题