gpt4 book ai didi

swift - 将图像 URL 放入 Collection View 中

转载 作者:行者123 更新时间:2023-11-30 12:47:59 25 4
gpt4 key购买 nike

我正在尝试从 url 获取图像到我的 Collection View 中。可以显示的图像数量取决于阵列计数。

这是我的代码:

class CollectionViewController: UICollectionViewController {


@IBOutlet weak var searchBox: UITextField!


var imageArray:[String] = []


override func viewDidLoad() {
super.viewDidLoad()
}

let link = "https://www.googleapis.com/my_link_is_here"
guard let url = URL(string: link) else {

print("Error: cannot create URL")
return

}

let request = URLRequest(url: url)

URLSession.shared.dataTask(with: request) { data, response, error in

guard error == nil else {

print("error calling GET on /todos/1")
print(error!)
return
}

do{

guard let json = try JSONSerialization.jsonObject(with: data!, options: []) as? [String : Any] else {

print("\(error?.localizedDescription)")
return
}

//print("The todo is: " + json.description)
guard let todotitle = json["items"] as? [Any] else {
print("Could not get todo title from JSON")
return
}

let arrcnt = todotitle.count


var i = 0

while (i < arrcnt){

guard let todotitle1 = todotitle[i] as? [String: Any] else {
print("Could not get todo title1 from JSON")
return
}


guard let todotitle2 = todotitle1["image"] as? [String : Any] else {
print("Could not get todo title2 from JSON")
return
}

guard let todotitle3 = todotitle2["thumbnailLink"] as? String else {

// continue
print("Could not get todo title3 from JSON")
return

}

self.imageArray.append(todotitle3)

print(self.imageArray)

i += 1

}
}catch{
print("error trying to convert data to JSON")
return
}

}.resume()

}



/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/

// MARK: UICollectionViewDataSource

override func numberOfSections(in collectionView: UICollectionView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}


override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of items
return self.imageArray.count
}

问题是当我写“return self.imageArray.count”时它返回nil。它采用空数组的初始化值。如何计算附加后的最终数组。

最佳答案

您需要在 while 循环之后在主线程上重新加载 collectionView。此外,您还可以通过制作单个 guard 语句而不是多个语句并使用 for 循环而不是 while 来减少代码,因此整个代码将如下所示。

do{

guard let json = try JSONSerialization.jsonObject(with: data!, options: []) as? [String : Any] else {

print("\(error?.localizedDescription)")
return
}

//print("The todo is: " + json.description)
guard let array = json["items"] as? [[String : Any]] else {
print("Could not get todo title from JSON")
return
}
for dic in array {
guard let imageDic = dic["image"] as? [String : Any], let thumbnailLink = imageDic["thumbnailLink"] as? String else {
print("Could not get thumbnailLink")
return
}
self.imageArray.append(thumbnailLink)
}
//Now you need to reload the collectionView on main thread.
DispatchQueue.main.async {
self.collectionView.reloadData()
}
}

关于swift - 将图像 URL 放入 Collection View 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41377939/

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