2016-10-11 25 views
0

沒有信息如何在Swift中使用帶有基本認證的SDWebImage。你能告訴我,我該怎麼做?SDWebImage如何使用基本認證

例如,如果我用Alamofire,我可以做基本的授權:

Alamofire.request(.GET, URLString, encoding: .JSON) 
    .authenticate(user: user, password: password) 
.... 

我從Alamofire URL地址的照片。然後,我想使用SDWebImage獲取圖像,併爲按鈕設置背景使用此圖像。

我想我需要使用sd_setImageWithURL從服務器獲取圖像。

button.sd_setImageWithURL(NSURL(string: photo)!, forState: .Normal) 

但我有基本的身份驗證,需要傳輸用戶名和密碼。

+0

你是什麼意思的基本認證。解釋更多 –

+0

,並添加細節,你已經嘗試過... – BHendricks

+0

我添加了Alamofire示例 – Dim

回答

0
let sdDownloader = SDWebImageDownloader.shared() 
    sdDownloader.username = "username" 
    sdDownloader.password = "password" 
    let logoURL = UserDefaults.standard.value(forKey: "communityLogo") as? String 
    if let url = URL(string: logoURL!) { 
     sdDownloader.downloadImage(with: url, options: .continueInBackground, progress: { (_, _, _) in 

     }, completed: { (image, _, _, _) in 
      self.communityLogoImage.image = image 
      self.communityLogoImage.contentMode = .scaleAspectFit 
     }) 

    } 
+0

嗨,沒有。但我爲此使用了AlamofireImage。 – Dim

+0

我已經做到了這一點,如果你想你也可以試試這個 –