使用 kingfisher lib 插入授权头字段 [英] Insert Authorization Header field with kingfisher lib
本文介绍了使用 kingfisher lib 插入授权头字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 Kingfisher 显示来自 url 的图像,但我的端点需要授权标头.如何在 iOS 的 Kingfisher 或 SDWebImage 中使用这些 url?
I'm using Kingfisher to show image from url, but my endpoint requires a Authorization header. How to use these kind of url with Kingfisher or SDWebImage in iOS?
推荐答案
使用 Kingfisher,您需要创建一个请求修饰符(AnyModifier
类型)并将其作为参数传递到 options
.kf.setImage
方法的一部分,然后使用尾随闭包来实际设置图像.
With Kingfisher you need to make a request modifier (of type AnyModifier
) and pass it as a parameter in the options
part of the .kf.setImage
method, and then use the trailing closure to actually set the image.
示例:
import Kingfisher
let modifier = AnyModifier { request in
var r = request
// replace "Access-Token" with the field name you need, it's just an example
r.setValue(<YOUR_TOKEN>, forHTTPHeaderField: "Access-Token")
return r
}
let url = URL(string: <YOUR_URL>)
let iView = <YOUR_IMAGEVIEW>
iView.kf.setImage(with: url, options: [.requestModifier(modifier)]) { (image, error, type, url) in
if error == nil && image != nil {
// here the downloaded image is cached, now you need to set it to the imageView
DispatchQueue.main.async {
iView.image = image
}
} else {
// handle the failure
print(error)
}
}
这篇关于使用 kingfisher lib 插入授权头字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文