SDWebImage和设置自定义HTTP标头?

时间:2013-04-28 18:50:43

标签: uiimageview http-headers sdwebimage image-caching

我刚刚将用于缓存图像的代码从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;

似乎lib确实支持HTTP头。但是当我使用UIImageView + WebCache.h时,我看不到设置标题的选项。在我的代码中我打电话

[self.imageView setImageWithURL:[NSURL URLWithString:themeImageURL] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

有人可以告诉我如何设置HTTP标头吗?

5 个答案:

答案 0 :(得分:42)

我遇到了同样的问题,我试图制作:

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"];

答案 1 :(得分:2)

我知道它已经很老了,但无法分享对我有用的东西。我需要为标题logintoken设置登录标记值。所以,这段代码做了我想要的 -

NSString *loginToken = // Some method to fetch login token    
[SDWebImageDownloader.sharedDownloader setValue:loginToken forHTTPHeaderField:@"logintoken"];

答案 2 :(得分:2)

我正在使用基本身份验证并在username帮助上设置passwordsharedDownloader

SDWebImageDownloader *downloader = [SDWebImageDownloader sharedDownloader];
downloader.username = @"username";
downloader.password = @"password";

答案 3 :(得分:0)

Swift版本

let imageDownloader = SDWebImageDownloader.shared()
imageDownloader.setValue("Username", forHTTPHeaderField: "X-Oauth-Username")

答案 4 :(得分:0)

Swift 4.1

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)
                    }
                }
            })