SDWebImage 和设置自定义 HTTP 标头? [英] SDWebImage and setting custom HTTP headers?
问题描述
我刚刚将用于缓存图像的代码从 EGOImageCache 更改为 SDWebView.不幸的是,我不知道如何设置自定义 HTTP 标头,因为我必须发送身份验证才能获取图像.使用 EGOImageCache 很容易完成,因为我在适当的地方扩展了 NSURLRequest.但我不知道如何使用 SDWebView.framework 做到这一点.我看到了标题,我在 SDWebImageDownloader.h 中找到了包含
I´ve just changed my code for caching images away from EGOImageCache to SDWebView. Unfortunately i don´t know how to set custom HTTP headers as i have to send authentification to be able to fetch images. It was easy done with EGOImageCache as i´ve extended the NSURLRequest at the appropriate place. But i don´t know how to do that with the SDWebView.framework. I see the headers and i´ve found methods in SDWebImageDownloader.h containing
/**
* 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 时,我看不到设置标题的选项.在我的代码中,我调用了
It seems that the lib does support HTTP headers. But as i use UIImageView+WebCache.h i can´t see there an option for setting the headers. In my code i call
[self.imageView setImageWithURL:[NSURL URLWithString:themeImageURL] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
谁能告诉我如何设置 HTTP 标头?
Can anybody tell me how to set HTTP headers?
推荐答案
我也遇到了同样的问题,我试着做:
I had the same problem, and I tried to make:
SDWebImageDownloader *manager = [SDWebImageDownloader sharedDownloader];
[manager setValue:username forHTTPHeaderField:@"X-Oauth-Username"];
但是标头没有发送.经过一些尝试,我遇到了问题,sharedDownloader 的 SDWebImageDownloader 会创建一个新的 SDWebImageDownloader 实例,因此当您将标头放在该实例上时,真正下载图像的实例没有标头.
But the header were not send. After some tries, I came across the problem, SDWebImageDownloader at sharedDownloader makes a new instance of SDWebImageDownloader, so when you put the header at that instance, the instance that really downloads the image don't has the header.
我已经解决了这个问题:
I've solved making this:
SDWebImageDownloader *manager = [SDWebImageManager sharedManager].imageDownloader;
[manager setValue:username forHTTPHeaderField:@"X-Oauth-Username"];
这篇关于SDWebImage 和设置自定义 HTTP 标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!