SDWebImage and setting custom HTTP headers?

前端 未结 5 734
余生分开走
余生分开走 2020-12-14 18:00

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 authentificati

相关标签:
5条回答
  • 2020-12-14 18:31

    Swift Version

    let imageDownloader = SDWebImageDownloader.shared()
    imageDownloader.setValue("Username", forHTTPHeaderField: "X-Oauth-Username")
    
    0 讨论(0)
  • 2020-12-14 18:32

    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)
                        }
                    }
                })
    
    0 讨论(0)
  • 2020-12-14 18:39

    I had the same problem, and I tried to make:

    SDWebImageDownloader *manager = [SDWebImageDownloader sharedDownloader];
    [manager setValue:username forHTTPHeaderField:@"X-Oauth-Username"];
    

    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"];
    
    0 讨论(0)
  • 2020-12-14 18:40

    I am using Basic authentication and setting the username and password on the sharedDownloader helped:

    SDWebImageDownloader *downloader = [SDWebImageDownloader sharedDownloader];
    downloader.username = @"username";
    downloader.password = @"password";
    
    0 讨论(0)
  • 2020-12-14 18:41

    I know it's pretty old but couldn't help to share what worked for me. I needed to set a login token value for header logintoken. So, this piece of code did what I wanted -

    NSString *loginToken = // Some method to fetch login token    
    [SDWebImageDownloader.sharedDownloader setValue:loginToken forHTTPHeaderField:@"logintoken"];
    
    0 讨论(0)
提交回复
热议问题