xcode 在 Swift 中获取按钮图像文件名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31975858/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 07:40:02  来源:igfitidea点击:

Get Button Image File Name in Swift

iosxcodeswift

提问by plumwd

Currently have a button with an embedded image. I'd like to get the image name of the button so that I can create a UIImage with it for sharing when the user clicks the button.

目前有一个带有嵌入图像的按钮。我想获取按钮的图像名称,以便在用户单击按钮时可以使用它创建一个 UIImage 以进行共享。

I've tried a variety of methods including

我尝试了多种方法,包括

let imagename = sender.currentImage
let image = UIImage(name: imagename)

This results in error "Missing argument for parameter 'inBundle' in call

这会导致错误“在调用中缺少参数‘inBundle’的参数

Looking for some guidance on how to proceed. I am going to have many buttons similar to this one and would rather build a function than individual actions for each button.

寻找有关如何进行的一些指导。我将有许多与此类似的按钮,并且宁愿为每个按钮构建一个功能而不是单独的操作。

回答by Ram Madhavan

Get the image name you have assigned to your UIButton:

获取分配给 UIButton 的图像名称:

let createdButton = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
createdButton.setImage(UIImage(named: "Image_Assigned"), for: .normal)
checkButtonName(yourButton: createdButton)

func checkButtonName(yourButton: UIButton) {
    if yourButton.currentImage == UIImage(named: "Image_Assigned") {
       print("IMAGE NAME IS Image_Assigned")
    }
    else {
       print("IMAGE NAME IS NOT Image_Assigned")
    }
 }

回答by Ram Madhavan

You can reuse the image using your first line:

您可以使用第一行重复使用图像:

let image = sender.currentImage

让图像 = sender.currentImage