Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/107.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 我如何在swift中获得按钮的图像?_Ios_Swift_Image_Button_Uiimagepickercontroller - Fatal编程技术网

Ios 我如何在swift中获得按钮的图像?

Ios 我如何在swift中获得按钮的图像?,ios,swift,image,button,uiimagepickercontroller,Ios,Swift,Image,Button,Uiimagepickercontroller,我使用此代码将图像设置为按钮 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage btnImg1.contentMode = .scaleAs

我使用此代码将图像设置为按钮

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)
}
现在我如何才能得到所选的图像,或者我的意思是如何得到按钮的图像?我想把图像保存到某个地方。。
请帮助,提前感谢

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

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

您只需使用以下代码即可从按钮获取图像:

    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)

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对象设置为UIButton,则可以访问UIImageView的所有属性,因为UIButton包含一个UIImageView来处理与图像相关的部分

是否正在查找btnImg1.imageView?.imageIn didFinishPickingMediaWithInfo方法当您将图像设置为button对象并将其副本保存到另一个uiimage对象时要从uiiimage对象获取图像,只需将图像存储在图像的全局对象中,首先,您应该在
UIButton
上的
touchUpInside
事件中添加一个目标,然后您可以通过
let image:UIImage=yourButton.image(用于:.normal)检索图像@RoohAl mahaba如果我的答案是你想要的,请将其标记为正确答案