gpt4 book ai didi

json - 为什么在swift中从json获取collectionView图像中的nil

转载 作者:行者123 更新时间:2023-11-28 13:26:18 24 4
gpt4 key购买 nike

我有带有图像和标签的 collectionview...我能够显示从 json 到标签的文本值,并且我正在获取从 json 到 cellForItemAtindexPath 的所有 img url,但所有这些图像都是我无法在 collectionview 中显示。我在 cellForItemAtindexPath 中有所有 20 个图像 url,我可以在控制台中看到它,但为什么我无法在 collectionview 中显示它们。

这是我的代码:

 import UIKit
import SDWebImage
struct JsonData {

var iconHome: String?
var typeName: String?
var id: String?
init(icon: String, tpe: String, id: String) {
self.iconHome = icon
self.typeName = tpe
self.id = id
}
}

class HomeViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UITextFieldDelegate, URLSessionDelegate {

@IBOutlet weak var collectionView: UICollectionView!
var itemsArray = [JsonData]()
override func viewDidLoad() {
super.viewDidLoad()
homeServiceCall()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return itemsArray.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! HomeCollectionViewCell

let aData = itemsArray[indexPath.row]
cell.paymentLabel.text = aData.typeName
cell.paymentImage.sd_setImage(with: URL(string:aData.iconHome!)) { (_, error, _, _) in
if let error = error {
print(error)
}
}
print("tableview collection images \(String(describing: aData.iconHome))")
return cell
}
//MARK:- Service-call

func homeServiceCall(){

let urlStr = "https://********/webservices//getfinancer"
let url = URL(string: urlStr)
URLSession.shared.dataTask(with: url!, completionHandler: {(data, response, error) in

guard let respData = data else {
return
}
guard error == nil else {
print("error")
return
}
do{
let jsonObj = try JSONSerialization.jsonObject(with: respData, options: .allowFragments) as! [String: Any]
let financerArray = jsonObj["financer"] as! [[String: Any]]

for financer in financerArray {
guard let id = financer["id"] as? String else { break }
guard let pic = financer["icon"] as? String else { break }
guard let typeName = financer["tpe"] as? String else { break }
print("the json icons \(String(describing: pic))")

let jsonDataObj = JsonData(icon: pic, tpe: typeName, id: id)
self.itemsArray.append(jsonDataObj)
}
DispatchQueue.main.async {
self.collectionView.reloadData()
}
}
catch {
print("catch error")
}
}).resume()
}
}

当我在 cellForItemAtindexPath 中打印图像 url 时,我在控制台中获得了所有 20 个图像 url.. 但为什么我无法在 collectionview 中显示它们。

我得到如下所示的输出.. 如果我在 sd_setImage 中提供占位符图像,则有些图像正在显示,有些则没有显示,然后它会在 collectionview 中显示占位符图像,为什么?

这是我的输出:

enter image description here

有些图像会出现,有些不会,但是服务器中没有 nil 图像所有图像都以 json 格式出现。我被困在这里很长时间了。任何人都请在这里帮助我。

最佳答案

因为您使用的是 Swift,所以我建议您使用 KingFisher而不是 SDWebImage 来处理来自 url 的图像。

我检查了你的代码,一切正常。但是,当您从 url 加载图像时,其中一些会抛出此错误:

发生 URL session 错误。底层错误: Error Domain=NSURLErrorDomain Code=-1202\“此服务器的证书无效。您可能正在连接到一个伪装成“anyemi.com”的服务器,这可能会使您的 secret 信息面临风险。”

此错误发生在域 anyemi.com 的 url 上。例如: https://anyemi.com/PaySTAR/images/LSPUBLICSCHOOL_icon.png https://anyemi.com/PaySTAR/images/CDMA_icon.png

dev.anyemi.com 的 URL 运行良好。例如: https://dev.anyemi.com/maheshbank/icons/electricity.png https://dev.anyemi.com/maheshbank/icons/gas.png

因此问题出在后端的 SSL 配置中。目前,您可以将域为 anyemi.com 的 url 更改为 dev.anyemi.com 进行测试,我相信它会很好。

关于json - 为什么在swift中从json获取collectionView图像中的nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58300912/

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