2017-05-15 68 views
0

我正在使用sdimage庫在imageview和圖像url上顯示圖像。 當我使用PNG URL圖像,它完美的工作,但是當我使用.jpg圖像格式網址,然後它不顯示imageview圖像。 代碼: -如何在Swift 3.0中的Imageview上顯示.jpg url圖像

let url = URL.init(string: "http://yeorder.com/api1/img/product/1494571262Nestle Cerelac Baby Cereal With Milk Wheat And Orange.jpg") 
imageview.sd_setImage(with: url , placeholderImage: UIImage.init(named: "logo")) 
+0

在這裏你得到的輸出'url' –

+0

當我檢查瀏覽器中的URL它顯示的圖像,但是當我在我的ImageView使用URL它不到風度工作 –

回答

2

您的網址包含空格。 Xcode中亙古不變的URL

支持空格「http://yeorder.com/api1/img/product/1494571262Nestle Cerelac嬰兒牛奶麥片小麥,Orange.jpg」

您需要使用下面的代碼urlString的

var urlString = "http://yeorder.com/api1/img/product/1494571262Nestle Cerelac Baby Cereal With Milk Wheat And Orange.jpg" 

    urlString = urlString.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)! 

    let url = URL.init(string:urlString) 

    imageview.sd_setImage(with: url , placeholderImage: UIImage.init(named: "logo")) 

輸出變爲空白,以20% : 「http://yeorder.com/api1/img/product/1494571262Nestle%20Cerelac%20Baby%20Cereal%20With%20Milk%20Wheat%20And%20Orange.jpg

1

在圖片名稱有問題

試試這個

斯威夫特3

var urlString = "http://yeorder.com/api1/img/product/1494571262Nestle Cerelac Baby Cereal With Milk Wheat And Orange.jpg" 

urlString = urlString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)! 

let url = URL(string:urlString) 
imageview.sd_setImage(with: url, placeholderImage: UIImage(named: "logo")) 
+0

acutally你需要添加'stringByAddingPercentEncodingWithAllowedCharacters' –

+0

解決方案更新 –

+0

我添加「addingPercentEncoding(withAllowedCharacters:NSCharacterSet.urlQueryAllowed)!」在我的網址和完美的工作謝謝你 –