LoginSignup
1
2

More than 5 years have passed since last update.

Swift3 Kingfisher で画像取得エラーの際にエラー画像を表示する

Last updated at Posted at 2016-10-11

Kingfisher でエラー画像表示

URL を渡して画像を取得するのに Kingfisher を利用してて大変便利なのだが、取得エラーになった際のやり方がわかりにくかったのでメモ。

やり方

extension Kingfisher where Base: ImageView {
    func setImageWithError(url: URL) {
        setImage(with: url,
            completionHandler: { (_, error, _, _) in
                if let _ = error {
                    self.base.image = #imageLiteral(resourceName: "error")
                }
            }
        )
    }
}

completionHandler で error があれば self.base.image にエラー画像を渡してあげると良い。
setImage の引数に errorImage とかないかな〜と思ったけどなかった。

上記は Extension にしてて、 imageView.kf.setImageWithError(url) で叩けるようにしてある。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2