1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

SDWebImageでカスタムヘッダをつけてsd_setImageする

Posted at

ヘッダをつけて画像をダウンロードしたいときってあるよなァ!

環境

SDWebImage5.8.0以上

方法

sd_setImage(with:placeholderImage:options:context)を利用する

sd_setImage(with: url,
            placeholderImage: nil,
            context: [.downloadRequestModifier: SDWebImageDownloaderRequestModifier(headers: ["Foo": "Bar"])])

あるいはclosureを使って

sd_setImage(with: url,
            placeholderImage: nil,
            context: [.downloadRequestModifier: SDWebImageDownloaderRequestModifier {
                var request = $0
                request.setValue("Bar", forHTTPHeaderField: "Foo")
                return request
}])

また、全てのリクエストに共通のヘッダをつけたい場合は

SDWebImageDownloader.shared.setValue("Bar", forHTTPHeaderField: "Foo")

でカスタムヘッダを設定できる

参考元

https://github.com/SDWebImage/SDWebImage/pull/2990
https://stackoverflow.com/questions/16266462/sdwebimage-and-setting-custom-http-headers

1
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?