我刚刚将用于缓存图像的代码从 EGOImageCache 更改为 SDWebView。不幸的是,我不知道如何设置自定义 HTTP 标头,因为我必须发送身份验证才能获取图像。使用 EGOImageCache 很容易完成,因为我在适当的地方扩展了 NSURLRequest。但我不知道如何使用 SDWebView.framework 做到这一点。我看到了标头,并且在 SDWebImageDownloader.h 中找到了包含
的方法 /**
* Set a value for a HTTP header to be appended to each download HTTP request.
*
* @param value The value for the header field. Use `nil` value to remove the header.
* @param field The name of the header field to set.
*/
- (void)setValue:(NSString *)value forHTTPHeaderField:(NSString *)field;
/**
* Returns the value of the specified HTTP header field.
*
* @return The value associated with the header field field, or `nil` if there is no corresponding header field.
*/
- (NSString *)valueForHTTPHeaderField:(NSString *)field;
似乎该库确实支持 HTTP 标头。但是当我使用 UIImageView+WebCache.h 时,我看不到设置标题的选项。在我的代码中我调用
[self.imageView setImageWithURL:[NSURL URLWithString:themeImageURL] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
谁能告诉我如何设置 HTTP 标头?
我有同样的问题,我试着做:
SDWebImageDownloader *manager = [SDWebImageDownloader sharedDownloader];
[manager setValue:username forHTTPHeaderField:@"X-Oauth-Username"];
但是标题没有发送。经过一些尝试,我遇到了问题,sharedDownloader 的 SDWebImageDownloader 创建了一个新的 SDWebImageDownloader 实例,因此当您将标头放在该实例时,真正下载图像的实例没有标头。
我已经解决了这个问题:
SDWebImageDownloader *manager = [SDWebImageManager sharedManager].imageDownloader;
[manager setValue:username forHTTPHeaderField:@"X-Oauth-Username"];
迅捷版
let imageDownloader = SDWebImageDownloader.shared()
imageDownloader.setValue("Username", forHTTPHeaderField: "X-Oauth-Username")
我知道它已经很老了,但忍不住分享对我有用的东西。我需要为标头
logintoken
设置登录令牌值。所以,这段代码做了我想要的 -
NSString *loginToken = // Some method to fetch login token
[SDWebImageDownloader.sharedDownloader setValue:loginToken forHTTPHeaderField:@"logintoken"];
我正在使用基本身份验证并在
username
上设置 password
和 sharedDownloader
帮助:
SDWebImageDownloader *downloader = [SDWebImageDownloader sharedDownloader];
downloader.username = @"username";
downloader.password = @"password";
let manager = SDWebImageManager.shared().imageDownloader
manager?.setValue("oAuthToken",forHTTPHeaderField: "AuthHeaderName")
manager?.downloadImage(with: imageURL, options: SDWebImageDownloaderOptions.useNSURLCache, progress:
{ (receivedSize, expectedSize , url) in
// progression tracking code
}, completed: { (image,data , error,finished) in
if error == nil && image != nil {
// here the downloaded image is cached, now you need to set it to the imageView
DispatchQueue.main.async {
imageView.image = image
self.maskCircle(anyImage: image!)
}
} else {
// handle the failure
DispatchQueue.main.async {
let defaultImage = UIImage(named: "defaultImage")
imageView.image = defImage
self.maskCircle(anyImage: defImage)
}
}
})
SDWebImage 5 以后需要使用以下代码片段。
func configureSDWebImageHeader(){
let requestModifier = SDWebImageDownloaderRequestModifier { (request) -> URLRequest? in
var mutableRequest = request
// Used Your Header Keys
mutableRequest.setValue("", forHTTPHeaderField: KeyConstant.USERID)
mutableRequest.setValue("", forHTTPHeaderField: KeyConstant.AUTH_KEY)
return mutableRequest
};
SDWebImageDownloader.shared.requestModifier = requestModifier
}