gpt4 book ai didi

ios - SDWebImage错误下载的图像有0像素

转载 作者:行者123 更新时间:2023-12-01 18:35:49 25 4
gpt4 key购买 nike

使用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)

}

由于政策问题,我的网址看起来像这样 无法共享确切的网址域名

https://tximages.mydomain.in/retailers_data_v2/retailer/320/3306-1-Aviator.jpg

以下是错误代码
Error Domain=NSURLErrorDomain Code=-1100 "(null)"

另外,我也可以在网络浏览器中打开图像URL。

这到底是什么问题?

以下扩展用于编码url
extension String {
var encoded: String {
return self.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? ""
}
}

更新:能够在Chrome浏览器中打开上方图片网址,但由于某种原因而无法在Safari浏览器中打开它吗?

最佳答案

-1100表示NSURLErrorFileDoesNotExist。还有一种可能是服务器不返回文件to specific country或由于urlString中的specific symbols而导致。在这种情况下,您可以应用以下编码:stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding
您还可以按照SDWebImageRetryFailed中所述使用documentation,更多有关this flag的信息:

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
}
}

关于ios - SDWebImage错误下载的图像有0像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59472312/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com