3

code is:

let url = URL(string: (user?.avatar)!)!
    print(url.absoluteString)
    let resource = ImageResource(downloadURL: url, cacheKey: "my_avatar")
    testUIButton.kf.setImage(with: resource, for:.normal)

The result is testUIButton display TintColor,No Image will be displayed.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Hayden
  • 449
  • 4
  • 13

3 Answers3

4

Try To Set UIButton Type Custom. This works for me.

let btnCustom = UIButton(type: .custom)
        
 if let imgURL = URL(string: imgURLString) {
       btnCustom.kf.setImage(with: imgURL, for: .normal)
 } else {
     btnCustom.setImage(UIImage(named: "img_user"), for: .normal)
 }
    
 btnCustom.cornerRadius = 15
 btnCustom.clipsToBounds = true
 btnCustom.addTarget(self, action: #selector(self.customAction)), for: .touchUpInside)
 view.addSubview(btnCustom) 

        
Ashish
  • 706
  • 8
  • 22
2

Final result is we must set a placeholder image for ImageButton.It seems to be same as in Android dev envrionment.

Hayden
  • 449
  • 4
  • 13
0

Try to use modifier as below:

let modifier = AnyImageModifier { return $0.withRenderingMode(.alwaysOriginal) }
button.kf.setImage(with: url, for: .normal, placeholder: nil, options: [.imageModifier(modifier)], progressBlock: nil, completionHandler: nil)
landonandrey
  • 1,271
  • 1
  • 16
  • 26