1.图片urlString后台返回的是
http://image.so.com/v?q=资格证书图&src=tab_www&correct=资格证书图&cmsid=30a27d740fe32537f4350602030ca0d7&cmran=0&cmras=6&cn=0&gn=0&kn=0#multiple=0&gsrc=1&dataindex=7&id=f742c8faa9ae2336fb8ef03a291fd667&currsn=0&jdx=7&fsn=60
但是SDWebImage加载不出来
但是 浏览器能正常显示
采用block打印日志
currentCell.graduateMajorImageV.sd_setImage(with: URL.init(string:imageUrl), placeholderImage: placeHoderImg, options: [], progress: { (num1, num2) in
}, completed: { (image, error, type, url) in
print("image",image,"error",error,"type",type,"url",url)
})
报错是 url 为nil
说明这个string 无法转为URL
2.看了一下有中文那就转译吧
方法 1
let strchinese_utf8_1 = "http://image.so.com/v?q=资格证书图&src=tab_www&correct=资格证书图&cmsid=30a27d740fe32537f4350602030ca0d7&cmran=0&cmras=6&cn=0&gn=0&kn=0#multiple=0&gsrc=1&dataindex=7&id=f742c8faa9ae2336fb8ef03a291fd667&currsn=0&jdx=7&fsn=60".wk_URLEncodedString3()
public func wk_URLEncodedString3() -> String{
let allowedCharacterSet = (CharacterSet(charactersIn: "!*'();:@&=+$,/?%#[]").inverted)
if let escapedString = self.addingPercentEncoding(withAllowedCharacters: allowedCharacterSet) {
return escapedString
}
return ""
}
结果
error "unsupported URL"
url 转译后是(
http%3A%2F%2Fimage.so.com%2Fv%3Fq%3D%E8%B5%84%E6%A0%BC%E8%AF%81%E4%B9%A6%E5%9B%BE%26src%3Dtab_www%26correct%3D%E8%B5%84%E6%A0%BC%E8%AF%81%E4%B9%A6%E5%9B%BE%26cmsid%3D30a27d740fe32537f4350602030ca0d7%26cmran%3D0%26cmras%3D6%26cn%3D0%26gn%3D0%26kn%3D0%23multiple%3D0%26gsrc%3D1%26dataindex%3D7%26id%3Df742c8faa9ae2336fb8ef03a291fd667%26currsn%3D0%26jdx%3D7%26fsn%3D60
结论 这个方法转译不对!!
方法2
let strchinese_utf8_2 = "http://image.so.com/v?q=资格证书图&src=tab_www&correct=资格证书图&cmsid=30a27d740fe32537f4350602030ca0d7&cmran=0&cmras=6&cn=0&gn=0&kn=0#multiple=0&gsrc=1&dataindex=7&id=f742c8faa9ae2336fb8ef03a291fd667&currsn=0&jdx=7&fsn=60".addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed) ?? ""
结果
error "Downloaded image has 0 pixels"
url是下面的
http://image.so.com/v?q=%E8%B5%84%E6%A0%BC%E8%AF%81%E4%B9%A6%E5%9B%BE&src=tab_www&correct=%E8%B5%84%E6%A0%BC%E8%AF%81%E4%B9%A6%E5%9B%BE&cmsid=30a27d740fe32537f4350602030ca0d7&cmran=0&cmras=6&cn=0&gn=0&kn=0%23multiple=0&gsrc=1&dataindex=7&id=f742c8faa9ae2336fb8ef03a291fd667&currsn=0&jdx=7&fsn=60
结论 这个尺寸为0 或者不能转为 图片
========================================================
let strchinese_utf8_3 = ("http://image.so.com/v?q=资格证书图&src=tab_www&correct=资格证书图&cmsid=30a27d740fe32537f4350602030ca0d7&cmran=0&cmras=6&cn=0&gn=0&kn=0#multiple=0&gsrc=1&dataindex=7&id=f742c8faa9ae2336fb8ef03a291fd667&currsn=0&jdx=7&fsn=60" as NSString).addingPercentEscapes(using: String.Encoding.utf8.rawValue) ?? ""
结果
error "Downloaded image has 0 pixels"
url 如下
http://image.so.com/v?q=%E8%B5%84%E6%A0%BC%E8%AF%81%E4%B9%A6%E5%9B%BE&src=tab_www&correct=%E8%B5%84%E6%A0%BC%E8%AF%81%E4%B9%A6%E5%9B%BE&cmsid=30a27d740fe32537f4350602030ca0d7&cmran=0&cmras=6&cn=0&gn=0&kn=0%23multiple=0&gsrc=1&dataindex=7&id=f742c8faa9ae2336fb8ef03a291fd667&currsn=0&jdx=7&fsn=60
============================================
方法2和方法3的结论一样,而且也是正确的中文转译方法!!
分析一下根本的原因
通过URL下载回来是一个无法转化为图片的数据.或者if (CGSizeEqualToSize(image.size, CGSizeZero)) 尺寸=0的时候才会报错
在浏览器中图片是能显示而且尺寸不是0,那推测这个url不是图片(对啊 没有.png .jpg结尾啊也!! 太二了!)
结果用postman 测试发现这tm的是一个网页 当然没法转成图片了!!!!
换个正常的图片链接是SDWebImage是没问题的,postman请求返回的也是图片
综上,中文转译的方法是2和3正确,图片加载不出来的根本原因是这个链接不是一个图片的url,是一个网页的地址...