21

我刚刚将用于缓存图像的代码从 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.hi 时,看不到设置标题的选项。在我的代码中,我调用

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

谁能告诉我如何设置 HTTP 标头?

4

5 回答 5

44

我有同样的问题,我试图使:

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"];
于 2013-05-23T08:22:39.343 回答
3

斯威夫特版本

let imageDownloader = SDWebImageDownloader.shared()
imageDownloader.setValue("Username", forHTTPHeaderField: "X-Oauth-Username")
于 2017-11-13T13:24:22.980 回答
2

我知道它已经很老了,但忍不住分享对我有用的东西。我需要为 header 设置登录令牌值logintoken。所以,这段代码做了我想要的——

NSString *loginToken = // Some method to fetch login token    
[SDWebImageDownloader.sharedDownloader setValue:loginToken forHTTPHeaderField:@"logintoken"];
于 2015-07-30T21:16:34.870 回答
2

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

SDWebImageDownloader *downloader = [SDWebImageDownloader sharedDownloader];
downloader.username = @"username";
downloader.password = @"password";
于 2016-11-27T20:05:46.100 回答
1

斯威夫特 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)
                    }
                }
            })
于 2018-08-21T11:11:28.340 回答