-1

This is my simple UIImageView extension:

extension UIImageView {

    func setImageWithString(string: String?) {

        if let string = string, let url = NSURL(string: string) {
            sd_setImageWithURL(url)
        }
    }
}

And for the following string I cannot get inside condition:

"http://posbistro-prod.s3.amazonaws.com/location_images/images/116/2fa/ad-/medium/Bez tytułu.jpg"

How can I workaround this?

Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358
  • Have you tried something with url percentage encoding (stringByAddingPercentEscapesUsingEncoding) ? take a look at this as well http://stackoverflow.com/questions/26353388/stringbyaddingpercentescapesusingencodingnsutf8stringencoding-returns-optional – Miknash Jul 18 '16 at 14:22
  • 1
    try to encode your url, like: `let encodedPath = string.URLString.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())` – Duyen-Hoa Jul 18 '16 at 14:23

1 Answers1

3

do like

func setImageWithString(stringbyName: String?) {

     let urlStr = stringbyName.stringByAddingPercentEncodingWithAllowedCharacters(. URLQueryAllowedCharacterSet())

    if let  url = NSURL(string: urlStr) {
        sd_setImageWithURL(url)
    }
}

or use like

   func setImageWithString(stringbyName: String?) {

     let urlStr = stringbyName.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)

    if let  url = NSURL(string: urlStr) {
        sd_setImageWithURL(url)
    }
}

for additional information see this link

Community
  • 1
  • 1
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143