2017-09-11 39 views
0

我使用此代码我如何快速获取按钮的图像?

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

现在我怎样才能选定的图像或我的意思是如何得到该按钮的图像的图像设置为一个按钮?我想要的形象somewhare保存.. 请帮助,在此先感谢

+2

您是否正在寻找btnImg1.imageView?.image – Abhishek

+1

在didFinishPickingMediaWithInfo方法中,当您将图像设置为按钮对象时,将其副本保存到另一个uiimage对象并且当您想要将图像保存到某个地方只需从uiiimage对象获取图像。 –

+1

你只是在图像的全局对象中存储图像 – Jay

回答

1

的UIButton包含ImageView的,当你指定图像的UIImageView,你可以得到的是它的财产图像配

所以FO:从按钮回来r UIButton得到ImageView然后图像

button.imageView?.image会给图像分配给UIButton 如果你的按钮有不同的图像状态,你也可以使用。

button.image(for: .normal or what ever state) 
    button.imageView?.image 

(如果设置图像的。中性双方将给予同样的结果)

enter image description here

@Rooh铝mahaba如果你有UIButton的对象的UIButton包含您可以访问的UIImageView的所有财产一个UIImageView来处理图像相关部分

+0

非常感谢你,那正是我想要的 –

+0

@ RoohAl-mahaba如果我的答案是你正在寻找的,请将它标记为正确答案 – Abhishek

1

您可以使用下面的代码来获取按钮的图像:

let btnImag = btnImg1.image(for: .normal) 
3

您可以简单地使用下面的代码来获得图像

let image = UIImage(named: "testImage") 
    let button = UIButton(frame: CGRect(x: 0.0, y: 0.0, width: 200.0, height: 200.0)) 
    button.setImage(image, for: UIControlState.normal) 

    //Get Image Back from Button 
    let imageFromButton : UIImage = button.image(for: UIControlState.normal)