arrays - 如何将图像从另一个类传输到 UIImage 数组?
问题描述
所以我有一个可以从相机创建的图像,我想将它传输到另一个类,该类的 UIImage 类型已经有一些东西。该数组连接到 UICollectionView。
我不知道该怎么做我仍然知道 swift 和一般的编程,但我只是喜欢学习。如果有人有任何建议,我将不胜感激。
这是collectionView的功能
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! DiscoverCollectionViewCell
cell.titleLable.text = Stuff[indexPath.item]
cell.imageView.image = stuffImages[indexPath.item]
return cell
}
下面是我创建的数组
let stuffImages : [UIImage] = [
UIImage(named: "1")!,
UIImage(named: "2")!,
UIImage(named: "3")!,]
这是我徒劳的尝试在另一堂课
var discover = MainViewController()
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
let image = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
imageCam.image = image
discover.stuffImages[]
picker.dismiss(animated: true, completion: nil)
}
我只是迷路了,非常感谢你的帮助谢谢
解决方案
将 stuffImages 数组更改为静态变量
static var stuffImages : [UIImage] = [
UIImage(named: "1")!,
UIImage(named: "2")!,
UIImage(named: "3")!,]
当你想添加一个新的图像追加到这个数组
MainViewController.stuffImages.append(image)
在 MainViewController 中重新加载集合视图
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.collectionView.reloadData()
}
推荐阅读
- reactjs - react hooks 可以确保 setState 和 props 在组件生命周期中始终是同一个实例吗?
- apache-kafka - 当消费者订阅正则表达式主题模式时,如何获取准确的 kafka 主题名称?
- quickfixj - Quickfixj发起者在互联网断开后重新连接到会话
- html - 如何防止第二列中的文本移动?如图所示
- c - 使用 write() 系统调用生成新行
- uwp - 如果终点为 0,CompositeTransform.TranslateY 不起作用
- java - 替换二维对象数组的元素
- kotlin - 从 Kotlin 代码构建的 jar 库是否仍然具有访问本地 Kotlin 代码等所有功能
- ios - Obj-C - NSPredicate 过滤器键在其标题中包含空格?
- ios - 如何判断我在视图中拖动的位置?