如何将图像从另一个类传输到UIImage数组?

时间:2019-04-25 12:58:43

标签: arrays swift function class

所以我有一个可以从相机创建的图像,我想将其传输到类型为UIImage的另一个类中,该类已经包含了一些东西。该数组已连接到UICollectionView。

我不知道该怎么做,但我仍然知道我通常会迅速地进行编程,但是我只是喜欢学习。如果有人有任何建议,我将不胜感激。

这是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)

    }

我只是迷路了,非常感谢您的帮助

2 个答案:

答案 0 :(得分:0)

将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()
}

答案 1 :(得分:0)

我要感谢您的工作。

这是collectionView的功能

 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    print("tapped \(indexPath.item)")
    var val = array[indexPath.item] //val is the which is going to give you the exact one
    let storyBoard : UIStoryboard = UIStoryboard(name:"Main", bundle :nil)
    let newVC = storyBoard.instantiateViewController(withIdentifier : "image") as! ViewControllerImage
    newVC.array.append(value) //Line used to append a value
    self.present(newVC, animated: true, completion: nil)
}

这是另一堂课

在这里,您已经创建了一个字符串数组,唯一的区别是UIImage

import UIKit
class ViewControllerImage: UIViewController, UITextViewDelegate {
@IBOutlet weak var imgz: UIImageView!

var array : [String] = [
"hello"
]

override func viewDidLoad() {
    super.viewDidLoad()
    print("value appended \(array)")       
}

我希望它是有用的...我想再次感谢您的学习努力