ios - Button background Image in swift 3 -
i add background image button in swift. actual image size 100x100. idea when click on image, popup of change profile image appear. then, can choose photos gallery , save.
after saving photo, image shows fully. but, problem before choose photo gallery, set default image. please see in following screenshot image.
profile female image should big background green color. here 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 1@-> 40x40 px , 2@-> 80x80px.
please me how solve problem.
you should not set default image here. should have uiimagepickercontrollerdelegate because you're opening photo gallery have add protocol function imagepickercontroller didfinishpickingmediawithinfo
func imagepickercontroller(_ picker: uiimagepickercontroller, didfinishpickingmediawithinfo info: [string : any]) { // if user has selected image gallery use if let profileimage = info[uiimagepickercontrollereditedimage] uiimage { // use selected image gallery here btnprofile.setimage(profileimage, for: .normal) } else { // else use 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 } } }
Comments
Post a Comment