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;
似乎lib确实支持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屋!