火力地堡存储。下载图像数组
Firebase storage. Download array of images
我正在尝试从 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
}
}
}
但是它有一些错误,比如
Cannot assign to property: i is a let constant ... and .... Cannot
assign value of type UIImage? to type String
如何更改我的型号或要求?
我在 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_metrics_log_metric_block_invoke(153) Failed to log metrics
我还有方法可以帮助我将模型保存到另一个模型。这就是为什么我想在我的模型中使用 String
这里..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()
}
您想直接访问图像数组,而不是对每个项目的引用。
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
中存储了 1 张图像,因此可以将变量名称更改为单数 image
,如下所示:
struct ImageModel {
var image: UIImage // <-- from String to UIImage
var index: Int
var imageLabel: String
var description: String
var comment = [String]()
}
让我知道这是有道理的
我正在尝试从 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
}
}
}
但是它有一些错误,比如
Cannot assign to property: i is a let constant ... and .... Cannot assign value of type UIImage? to type String
如何更改我的型号或要求?
我在 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_metrics_log_metric_block_invoke(153) Failed to log metrics
我还有方法可以帮助我将模型保存到另一个模型。这就是为什么我想在我的模型中使用 String
这里..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()
}
您想直接访问图像数组,而不是对每个项目的引用。
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
中存储了 1 张图像,因此可以将变量名称更改为单数 image
,如下所示:
struct ImageModel {
var image: UIImage // <-- from String to UIImage
var index: Int
var imageLabel: String
var description: String
var comment = [String]()
}
让我知道这是有道理的