按钮背景图像在迅速3

时间:2017-04-07 07:12:09

标签: ios swift3 uibutton imagebutton

我在swift中将背景图片添加到按钮。实际图片尺寸 100x100 。我的想法是,当我点击图像时,会出现更改个人资料图像的弹出窗口。然后,您可以从图库中选择照片并保存。
保存照片后,图像完全显示。但是,问题是在我从图库中选择照片之前,我设置了默认图像。请参阅以下截图图片。

enter image description here

个人资料女性形象应该大到背景绿色。这是我的代码。

let img = UIImage(data: match.value(forKey: "imageData") as! Data )
btnProfile.frame = CGRect(x: 10, y: 100, width: 100, height: 100)
btnProfile.imageView?.contentMode = UIViewContentMode.scaleToFill
btnProfile.setImage(img, for: UIControlState.normal)

if(user_gender == "Female"){

      btnProfile.setImage(UIImage(named: "femaleprofile_image")?.withRenderingMode(.alwaysOriginal), for: .normal)
      btnProfile.imageView?.contentMode = UIViewContentMode.scaleToFill
}

图片尺寸 1 @ - > 40x40 px和 2 @ - > 80×80 像素。
请有人帮我解决这个问题。

1 个答案:

答案 0 :(得分:1)

您不应在此处设置默认图片。您应该已经拥有UIImagePickerControllerDelegate,因为您要打开照片库所有您需要做的就是添加协议函数imagePickerController didFinishPickingMediaWithInfo

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    // if user has selected an image from gallery use that
    if let profileImage = info[UIImagePickerControllerEditedImage] as UIImage {
           // use the selected image from gallery here
           btnProfile.setImage(profileImage, for: .normal)
        } else {
           // else use your logic
           let img = UIImage(data: match.value(forKey: "imageData") as! Data )
                btnProfile.frame = CGRect(x: 10, y: 100, width: 100, height: 100)
                btnProfile.imageView?.contentMode = UIViewContentMode.scaleToFill
                btnProfile.setImage(img, for: UIControlState.normal)

           if(user_gender == "Female"){
              btnProfile.setImage(UIImage(named:"femaleprofile_image")?.withRenderingMode(.alwaysOriginal), for: .normal)
              btnProfile.imageView?.contentMode = UIViewContentMode.scaleToFill
           }

    }
}