提问人:amodkanthe 提问时间:12/25/2019 最后编辑:amodkanthe 更新时间:4/8/2021 访问量:1020
SDWebImage 错误下载的图像有 0 像素
SDWebImage error downloaded image has 0 pixels
问:
使用 SDWebImage 最新的 swift 库版本在加载几个 url 时出现以下错误
下载的图像有 0 像素
以下是代码
if let url = URL(string: storeImgStr.encoded), !(storeImgStr.isEmpty) {
self.storeImg.sd_setImage(with: url, placeholderImage: UIImage(named: Constants.storeImagePlaceorderImage)) {
(image, error, cahce, url) in
if(error != nil) {
}
}
}
else {
self.storeImg.image = UIImage(named: Constants.storeImagePlaceorderImage)
}
我的 URL 看起来像这样,由于政策问题,无法共享确切的 URL,域名已更改
https://tximages.mydomain.in/retailers_data_v2/retailer/320/3306-1-Aviator.jpg
以下是错误代码
Error Domain=NSURLErrorDomain Code=-1100 "(null)"
我还可以在网络浏览器中打开图像网址。
这里到底有什么问题?
以下用于编码 url 的扩展名
extension String {
var encoded: String {
return self.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? ""
}
}
更新:能够在Chrome浏览器中打开上面的图像URL,但无法在Safari浏览器中打开它,有什么原因吗?
答:
3赞
Aleksey Potapov
12/27/2019
#1
-1100
方法。还有一种可能性是,服务器不会将文件返回到特定国家/地区,或者由于 .在这种情况下,您可以应用以下编码: .
还可以使用 SDWebImageRetryFailed,如文档中所述,详细了解此标志:NSURLErrorFileDoesNotExist
urlString
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding
storeImg.sd_setImage(with: urlEncoded, placeholderImage: UIImage(named: Constants.storeImagePlaceorderImage), options: .retryFailed) { // handle result completion }
您可以使用以下方法测试断开的链接:
import UIKit
import SDWebImage
import WebKit
extension String {
var encoded: String {
return self.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? ""
}
}
class ViewController: UIViewController {
let testURL = "https://i.groupme.com/638x640.jpeg.d4f31c747b534baca03d12db5a2b6193"
let testURLShouldEncode = "https://via.placeholder.com/468x60?text=Visit Blogging.com Now"
var storeImg: UIImageView!
var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
configureUI()
if let urlEncoded = URL(string: testURLShouldEncode.encoded), !(testURL.isEmpty) {
storeImg.sd_setImage(with: urlEncoded, placeholderImage: UIImage(named: Constants.storeImagePlaceorderImage)) {
[unowned self] (image, error, cahce, url) in
print("image \(String(describing: image))")
print("error \(String(describing: error?.localizedDescription))")
print("cache \(cahce)")
print("url \(String(describing: url))")
if (error != nil) {
let request = URLRequest(url: urlEncoded)
self.webView.load(request)
}
}
} else {
self.storeImg.image = UIImage(named: Constants.storeImagePlaceorderImage)
}
}
func configureUI() {
storeImg = UIImageView()
storeImg.contentMode = .center
storeImg.translatesAutoresizingMaskIntoConstraints = false
webView = WKWebView()
webView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(storeImg)
view.addSubview(webView)
view.backgroundColor = .blue
storeImg.backgroundColor = .yellow
webView.backgroundColor = .orange
storeImg.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
storeImg.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
storeImg.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
storeImg.heightAnchor.constraint(equalToConstant: 300).isActive = true
webView.topAnchor.constraint(equalTo: storeImg.safeAreaLayoutGuide.bottomAnchor).isActive = true
webView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
webView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
webView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
}
}
0赞
M.Bonjour
4/8/2021
#2
我也遇到了这个问题,我的解决方案是通过添加到您的目标 -> -> 中来完成的。"SD_WEBP=1"
SDWebImage
Build Settings
Preprocessor Macros
评论
encoded
String
encoded
没问题。使用此链接进行测试 - 将其完美地编码为 .问题出在你是图像链接上。https://via.placeholder.com/468x60?text=Visit Blogging.com Now
https://via.placeholder.com/468x60?text=Visit%20Blogging.com%20Now