Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Button Image File Name in Swift

Tags:

xcode

ios

swift

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.

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

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.

like image 442
elke_wtf Avatar asked Dec 01 '22 17:12

elke_wtf


1 Answers

Get the image name you have assigned to your 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")
    }
 }
like image 131
Ram Madhavan Avatar answered Dec 09 '22 16:12

Ram Madhavan