如何从UIButton获取图像?

时间:2022-10-30 23:45:58

I have Custom Buttons in my application in which I have set images. I want to get image from button and stored in UIImage variable. Kindly Guide me.

我在我的应用程序中有自定义按钮,我在其中设置了图像。我想从按钮获取图像并存储在UIImage变量中。请指引我。

3 个解决方案

#1


56  

How about this?

这个怎么样?

UIImage *img = [button imageForState:UIControlStateNormal];

#2


1  

Swift 3.0 syntax

斯威夫特3.0语法

let image: UIImage = yourButton.image(for: .normal)!

#3


0  

UIImage *myNewImage = [UIButton imageForState:UIControlStateNormal];

UIImage *myNewImage = [UIButton imageForState: uicontrolstatal];

Though this isn't very good design practice. I would do this:

虽然这不是很好的设计实践。我想这样做:

UIImage *myNewImage = [UIImage imageNamed:@"name of image that is in your bundle"];

UIImage *myNewImage = [UIImage imageNamed:@"在你的包中的图像名称"];

#1


56  

How about this?

这个怎么样?

UIImage *img = [button imageForState:UIControlStateNormal];

#2


1  

Swift 3.0 syntax

斯威夫特3.0语法

let image: UIImage = yourButton.image(for: .normal)!

#3


0  

UIImage *myNewImage = [UIButton imageForState:UIControlStateNormal];

UIImage *myNewImage = [UIButton imageForState: uicontrolstatal];

Though this isn't very good design practice. I would do this:

虽然这不是很好的设计实践。我想这样做:

UIImage *myNewImage = [UIImage imageNamed:@"name of image that is in your bundle"];

UIImage *myNewImage = [UIImage imageNamed:@"在你的包中的图像名称"];