elkefreed
elkefreed

Reputation: 964

Get Button Image File Name in 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.

Upvotes: 5

Views: 10855

Answers (2)

Ram Madhavan
Ram Madhavan

Reputation: 2388

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")
    }
 }

Upvotes: 14

user5034511
user5034511

Reputation:

You can reuse the image using your first line:

let image = sender.currentImage

Upvotes: 6

Related Questions