簡體   English   中英

IOS Swift SDWebImage 未編碼 URL,如何為其設置編碼器?

[英]IOS Swift SDWebImage not encoding URL, how do I set an encoder for it?

我正在使用SDWebImage從 URL 加載圖像。

以下是我的代碼:

    if let imageUrl = URL(string: carouselItem.imagesArray[0]) {
    itemImageView.sd_setImage (with: imageUrl,placeholderImage: UIImage(named: "ImagePlaceorder"))
    } else {
        itemImageView.image = UIImage(named: "ImagePlaceorder")

    }

圖像未加載,因為 URL 中有空格。 我不想對每個單獨的 URL 進行編碼,我想為圖像加載器本身設置一個編碼器,以便 SDWebImage 在下載圖像之前對 URL 進行編碼。

如何將 URL 編碼器設置為 SDWebImage?

由於他們沒有這種可能性,因此您有幾種方法可以解決它。

  1. 由於它們的方法是在 UIImageView 的擴展中聲明的,因此您無法覆蓋它們(Swift doc )。 但是您可以為子類執行此操作:
class ImageVIew: UIImageView {

    override open func sd_setImage(with url: URL?, placeholderImage placeholder: UIImage?, options: SDWebImageOptions = [], completed completedBlock: SDExternalCompletionBlock? = nil) {
        // Apply URL encoding
        super.sd_setImage(with: newURL, placeholderImage placeholder, options: options, completed: completedBlock)
    }

}
  1. 創建一個fork,添加一個公共屬性,將MR推送到SDWebImage repo

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM