我使用这段代码将图像设置为按钮
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
btnImg1.contentMode = .scaleAspectFit
btnImg1.setImage(chosenImage, for: UIControlState.normal)
dismiss(animated: true, completion: nil)
}
现在如何获取所选图像或者我的意思是如何获取按钮的图像?我希望图像以某种方式保存.. 请帮助,提前致谢
最佳答案
UIButton 包含 ImageView,当您将图像分配给 UIImageView 时,您可以通过其 .image 属性获取 is
所以对于 UIButton 获取 ImageView 然后获取 Image
button.imageView?.image
会给出分配给 UIButton 的图像
如果您的按钮有不同的状态图像,您也可以使用 .
button.image(for: .normal or what ever state)
button.imageView?.image
(如果您使用 .normal 设置图像,两者将给出相同的结果)
@Rooh Al-mahaba 如果你有 UIButton 对象,你可以访问 UIImageView 的所有属性,因为 UIButton 包含一个 UIImageView 来处理图像相关部分
关于ios - 我如何快速获取按钮的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46150209/