-1

I add background image to button in swift. The actual image size is 100x100. The idea is that when I click on the image, the popup of change profile image will appear. Then, you can choose photos from gallery and save.
After saving the photo, image shows fully. But, the problem is before I choose the photo from gallery, I set default image. Please see in following screenshot image.

enter image description here

Profile Female image should be big as background green color. Here is my code.

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
}

Image size is 1@-> 40x40 px and 2@-> 80x80px.
Please anyone help me how to solve this problem.

May Phyu
  • 895
  • 3
  • 23
  • 47
  • Possible duplicate of [How do I scale a UIButton's imageView?](http://stackoverflow.com/questions/1957317/how-do-i-scale-a-uibuttons-imageview) – Vishnu gondlekar Apr 07 '17 at 07:46

1 Answers1

1

You should not set your default image here. You should already have the UIImagePickerControllerDelegate because you're opening the photo gallery all you have to do is add the protocol function 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
           }

    }
}
Khalid Afridi
  • 913
  • 5
  • 12
  • Bro @khalidAfridi, please let me explain of how my app works. When you run the app for the first time, the app will ask your gender status. If you click on Female image, then Female image will be display in it. Like upper screenshot image. Then, if you want to add your image, simply tap on the Female image and choose photo from your gallery. This is how my app works. – May Phyu Apr 07 '17 at 07:52
  • @MayPhyu still my answer is valid I think. Maybe I didn't understand well or you couldn't explained your question clearly you want to change the buttons image if a user selected a photo otherwise it shouldn't – Khalid Afridi Apr 07 '17 at 08:15
  • bro I can solve it now. Here is the code that I add. btnProfile.contentHorizontalAlignment = .fill btnProfile.contentVerticalAlignment = .fill – May Phyu Apr 07 '17 at 08:21