火箱存储。下载图像数组
我正在尝试从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
中使用我的模型。因此,如果它在内部有收集视图和单元格。单元具有图像和名称。我必须从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
}
存储
的
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: []),
]
}
会
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外交部[14378:13214993] [Boringssl] boringssl_metrics_log_metric_block_invoke(153)无法记录指标
我也有可以帮助我将模型保存到另一个模型的方法。 为什么我想在此处使用
。
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)
}
字符串
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()
}
I'm trying to download some images from Firebase Storage folder and pus it into my model Rules Firebase Folder Images in folder
My model is here. Instead of String (1,2,3,4,5) I have to use images from folder that has the same String names
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: []),
]
}
I have found how to write a request
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
}
}
}
But it has some errors, like
Cannot assign to property: i is a let constant ... and .... Cannot
assign value of type UIImage? to type String
How do I change my model or request?
I use my model in CollectionViewCell
. So I have collectionView and Cell inside if it. Cell has Image and Name. This image I have to get from Firebase Storage
and name I already have in my model. The reason is using Storage that my images can changes sometimes
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:
I change my model to:
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: []),
]
}
Then I put method to ViewWillAppear
and trying to get error or result in it
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)
}
}
And Nothing happens, logs are empty
2022-05-29 20:20:09.668785+0700 diplom[14378:13214993] [boringssl]
boringssl_metrics_log_metric_block_invoke(153) Failed to log metrics
Also I have method that helps me to save Model to another Model. Thats why I wanted to use String in my Model
Here it is.. CollectionView
prepare for 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
property
var pictureNamed = String()
Model of Saved Collection Cell
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
where I use codable model before
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) }
}
}
And here is my method. So there are a lot of changes... Is any resolution to get string of image?
@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()
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要直接访问图像数组,而不是对每个项目的引用。
另外,您的
图像
属性接受了一个字符串,如下所示:但是您正在尝试为其分配
uiimage
。我不确定您要添加到ImageModel
的内容中,也许您可以详细说明?回答更新的问题:
首先,允许您的结构接受
images
作为uiimage
:然后,更改此行:
TO:
和一个最终建议,看来您在
imageModel.images
中仅存储1个图像,因此也许将变量名称更改为单数image
so:让我知道这很有意义
You want to access the array of images directly, not the reference to each item.
Also, your
images
property accepts a String, as defined here:But you're trying to assign it a
UIImage
. I'm not sure what you're trying to add to theImageModel
, maybe you can elaborate?Response to updated question:
First, allow your struct to accept
images
as aUIImage
:Then, change this line:
To:
And one final suggestion, it seems you're storing only 1 image inside
ImageModel.images
, so maybe change the variable name to singularimage
like so:Let me know this makes sense