如何在Swift 3.0中的Imageview上显示.jpg url图像

时间:2017-05-15 11:28:37

标签: ios json swift image imageview

我正在使用sdimage库在imageview上使用图片网址显示图片。 当我使用png url图像时,它工作得很好但是当我使用.jpg图像格式的网址时,它不会在imageview上显示图像。 代码: -

 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"))

2 个答案:

答案 0 :(得分:2)

您的网址包含空格。 xcode不支持url中的空格

" http://yeorder.com/api1/img/product/1494571262Nestle Cerelac婴儿谷物与牛奶小麦和Orange.jpg"

您需要使用以下代码

将空格更改为%20
    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"))

输出urlString:" http://yeorder.com/api1/img/product/1494571262Nestle%20Cerelac%20Baby%20Cereal%20With%20Milk%20Wheat%20And%20Orange.jpg"

答案 1 :(得分:1)

在您的图片名称中有问题

试试这个

Swift 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"))