如何从CollectionView查找图像的名称?

时间:2018-11-28 15:03:23

标签: swift uicollectionview uicollectionviewcell

当我从CollectionView和此单元格中选择一个图像时,我想获取该图像的名称并将其保存到CloudKit。我的主要问题是如何获取所选图像的名称?

这是我的CollectionViewCell:

class AddImageViewCell: UICollectionViewCell {
    @IBOutlet weak var addListImageView: UIImageView!

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        self.layer.cornerRadius = self.frame.size.width * 0.2
        self.layer.borderWidth = 2
        self.layer.borderColor = UIColor.clear.cgColor
    }
}

我的一些AddListViewController:

class AddListViewController: UIViewController {
    var imageArray : [UIImage] = [UIImage(named: "Images/do001.png")!,
                                  UIImage(named: "Images/do002.png")!,
                                  UIImage(named: "Images/do003.png")!,
                                  UIImage(named: "Images/do004.png")!,
                                  UIImage(named: "Images/do005.png")!,
                                  UIImage(named: "Images/do006.png")!,
                                  UIImage(named: "Images/do007.png")!,
                                  UIImage(named: "Images/do008.png")!,
                                  UIImage(named: "Images/do009.png")!,
                                  UIImage(named: "Images/do010.png")!,
                                  UIImage(named: "Images/do011.png")!,
                                  UIImage(named: "Images/do012.png")!,
                                  UIImage(named: "Images/do013.png")!,
                                  UIImage(named: "Images/do014.png")!,
                                  UIImage(named: "Images/do015.png")!,
                                  UIImage(named: "Images/do016.png")!]

    let selectedImage = 0

在这里我要获取图像文件的名称:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let cell = addImageCollectionView.cellForItem(at: indexPath) as! AddImageViewCell
    cell.layer.borderColor = UIColor.white.cgColor

    var imageInCell = cell.addListImageView.image

    print("\(String(describing: imageInCell))")
}

此处CellForItemAt:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell : AddImageViewCell = addImageCollectionView.dequeueReusableCell(withReuseIdentifier: "AddImageCell", for: indexPath) as! AddImageViewCell
    cell.addListImageView.image = imageArray[indexPath.item]
    return cell
}

2 个答案:

答案 0 :(得分:0)

不从单元格中读取值。单元被重用。而是使用您的模型。

我建议这样做:

var imageNameArray: [String] = ["Images/do001.png", "Images/do002.png", ...]

collectionView(_:cellForItemAt:)中:

let imageName = imageNameArray[indexPath.item]
let image = UIImage(named: imageName)
cell.addListImageView.image = image

collectionView(_:didSelectItemAt:)中:

let selectedImageName = imageNameArray[indexPath.item]

答案 1 :(得分:-1)

您可以使用此扩展名

  1. 它创建属性name并使用自定义init设置其值,
  2. 如果您不使用自定义init,它将返回空的String
 extension UIImage {
    convenience init?(named: String, saveName: Bool) {
        self.init(named: named)
        if saveName {
            self.name = named
        }
    }
    struct Name {
        static var name: String = ""
    }
    var name: String {
        get {
            return Name.name
        }
        set(newValue) {
            Name.name = newValue
        }
    }
}

用法:

let img = UIImage(named: "test.png", saveName: true)

img?.name // "test.png"

// With image view
let imageView = UIImageView(image: img)
imageView.image?.name