代码是:
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,不会显示图片。
代码是:
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,不会显示图片。
尝试设置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)
最终结果是我们必须为 ImageButton 设置一个占位符图像。这似乎与 Android 开发环境中的相同。
尝试使用如下修饰符:
let modifier = AnyImageModifier { return $0.withRenderingMode(.alwaysOriginal) }
button.kf.setImage(with: url, for: .normal, placeholder: nil, options: [.imageModifier(modifier)], progressBlock: nil, completionHandler: nil)