翠鸟无法为 UIButton 设置图像



code是:

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显示色调颜色,不会显示任何图像。

尝试设置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.It 设置一个占位符图像,该图像似乎与Android开发环境中相同。

尝试使用修饰符,如下所示:

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

最新更新