3

代码是:

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

结果是testUIButton显示TintColor,不会显示图片。

4

3 回答 3

2

尝试设置UIButton类型Custom。这对我有用。

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) 

        
于 2020-11-02T08:39:38.583 回答
2

最终结果是我们必须为 ImageButton 设置一个占位符图像。这似乎与 Android 开发环境中的相同。

于 2017-11-05T13:17:34.510 回答
0

尝试使用如下修饰符:

let modifier = AnyImageModifier { return $0.withRenderingMode(.alwaysOriginal) }
button.kf.setImage(with: url, for: .normal, placeholder: nil, options: [.imageModifier(modifier)], progressBlock: nil, completionHandler: nil)
于 2020-12-14T14:22:48.480 回答