在从资产文件夹中获取的按钮上引用 UIImage 并且仍然需要图像继续

Referencing UIImage on a button taken from assets folder and still needing image going forward

我不会深入了解我的应用程序最终在做什么,但我遇到的问题是当我单击 6 个按钮中的一个时,它们的 imageView 中都有一个 UIImage 然后我尝试引用图像名称(图像来自我的资产)切换按下哪个按钮并使用结果更新标签。

正在设置值并在操作中打印出来,但是 switch 语句总是落入默认值,我不明白为什么。按钮 imageViews 是否影响图像名称?

@IBOutlet var feelingLabel: UILabel!

var feelingImage: UIImage? {
    didSet {
        switch feelingImage {
        case UIImage(named: "happyFace"):
            self.feelingLabel.text = "feeling happy"
        case UIImage(named: "fineFace"):
            self.feelingLabel.text = "feeling fine"
        case UIImage(named: "angryFace"):
            self.feelingLabel.text = "feeling angry"
        case UIImage(named: "anxiousFace"):
            self.feelingLabel.text = "feeling anxious"
        case UIImage(named: "sadFace"):
            self.feelingLabel.text = "feeling sad"
        case UIImage(named: "sickFace"):
            self.feelingLabel.text = "feeling sick"
        default:
            self.feelingLabel.text = "feeling..."
        }
    }
}

// all six buttons are connected to this action 
@IBAction func feelingButtonTapped(_ sender: UIButton) {
    guard let selectedFeelingImage = sender.imageView?.image,
          selectedFeelingImage != self.feelingImage else {
        self.feelingImage = nil
        return
    }
    self.feelingImage = selectedFeelingImage
    print(selectedFeelingImage)
    print(self.feelingImage)
}

如果要比较图像,需要将它们转换为 Data,如下所示:

var feelingImage: UIImage? {
didSet {
    switch feelingImage!.pngData() {
    case UIImage(named: "happyFace")!.pngData():
        self.feelingLabel.text = "feeling happy"
    case UIImage(named: "fineFace")!.pngData():
        self.feelingLabel.text = "feeling fine"
    case UIImage(named: "angryFace")!.pngData():
        self.feelingLabel.text = "feeling angry"
    case UIImage(named: "anxiousFace")!.pngData():
        self.feelingLabel.text = "feeling anxious"
    case UIImage(named: "sadFace")!.pngData():
        self.feelingLabel.text = "feeling sad"
    case UIImage(named: "sickFace")!.pngData():
        self.feelingLabel.text = "feeling sick"
    default:
        self.feelingLabel.text = "feeling..."
    }
}

}

A UIImage 没有“名称”...您的图像 resource 有,这就是您用来加载图像的名称。但是,一旦加载,就与资源名称没有关联。

虽然比较 .pngData 可能有效,但这是一种非常低效的方法。

一个非常好的方法是在一个数组中跟踪您的按钮,并使用数组索引来确定哪个按钮被点击。

另一种方法是继承 UIButton 并添加您自己的 .imageName 属性.

例如:

class NamedButton: UIButton {
    var imageName: String = ""
}

现在,在代码中,您创建的按钮变为:

    if let img = UIImage(named: "happyFace") {
        let btn = NamedButton()
        btn.setImage(img, for: [])
        btn.imageName = "happyFace"
        btn.addTarget(self, action: #selector(feelingButtonTapped(_:)), for: .touchUpInside)
    }

您的“当前选择”可以是:

var feelingImageName: String = "" {
    
    didSet {
        switch feelingImageName {
        case "happyFace":
            self.feelingLabel.text = "feeling happy"
        case "fineFace":
            self.feelingLabel.text = "feeling fine"
        case "angryFace":
            self.feelingLabel.text = "feeling angry"
        case "anxiousFace":
            self.feelingLabel.text = "feeling anxious"
        case "sadFace":
            self.feelingLabel.text = "feeling sad"
        case "sickFace":
            self.feelingLabel.text = "feeling sick"
        default:
            self.feelingLabel.text = "feeling..."
        }
    }

}

您的按钮操作可以是:

@IBAction func feelingButtonTapped(_ sender: UIButton) {
    
    guard let btn = sender as? NamedButton,
          btn.imageName != self.feelingImageName else {
        self.feelingImageName = ""
        return
    }
    self.feelingImageName = btn.imageName
    print(btn.imageName)
    print(self.feelingImageName)

}

如果您使用 Storyboard / Interface Builder 作为您的布局,您可以制作 .imageName 属性 @IBInspectable 这样您就可以在设计时设置它:

class NamedButton: UIButton {
    @IBInspectable var imageName: String = ""
}