Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在swift中更快地在CollectionView中加载JSON图像URL_Ios_Json_Swift_Uicollectionview_Uiimageview - Fatal编程技术网

Ios 如何在swift中更快地在CollectionView中加载JSON图像URL

Ios 如何在swift中更快地在CollectionView中加载JSON图像URL,ios,json,swift,uicollectionview,uiimageview,Ios,Json,Swift,Uicollectionview,Uiimageview,我有图像和标签的集合视图。。我能够在collection视图中非常缓慢地加载图像和标签值,我的意思是collection视图下载json img URL会花费很多时间。。但我需要更快地从json下载到集合视图。。。而且我也不会很快得到10张相同的图片。。我不知道为什么会这样。。任何人请帮助我在集合视图中以更快的速度下载json img url 这是我的密码: import UIKit struct JsonData { var iconHome: String? var typeName: S

我有图像和标签的集合视图。。我能够在collection视图中非常缓慢地加载图像和标签值,我的意思是collection视图下载json img URL会花费很多时间。。但我需要更快地从json下载到集合视图。。。而且我也不会很快得到10张相同的图片。。我不知道为什么会这样。。任何人请帮助我在集合视图中以更快的速度下载json img url

这是我的密码:

 import UIKit
struct JsonData {
var iconHome: String?
var typeName: String?
init(icon: String, tpe: String) {
    self.iconHome = icon
    self.typeName = tpe
}
}

class HomeViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UITextFieldDelegate {

@IBOutlet weak var collectionView: UICollectionView!

var itemsArray = [JsonData]()
var idArray = [Int]()
var homSe = [JsonDataHome]()
override func viewDidLoad() {
    super.viewDidLoad()

    homeServiceCall()
    //Do any additional setup after loading the view.
    collectionView.delegate = self
    collectionView.dataSource = self
}

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

    if let url = NSURL(string: aData.iconHome ?? "") {
        if let data = NSData(contentsOf: url as URL) {
            cell.paymentImage.image = UIImage(data: data as Data)
        }
    }
    return cell
}

//MARK:- Service-call
func homeServiceCall(){

    let urlStr = "https://dev.com/webservices"
    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]
            //print("the home json is \(jsonObj)")
            let financerArray = jsonObj["financer"] as! [[String: Any]]
            for financer in financerArray {

                let id = financer["id"] as? Int
                let pic = financer["icon"] as? String
                let typeName = financer["tpe"] as! String
                self.idArray.append(id ?? 1)

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

在这里,我需要在集合视图中快速下载json图像。

首先没有比
homeServiceCall()中的内容更快的东西了,这取决于您的internet连接

第二行此行

如果let data=NSData(contentsOf:url作为url){

阻止主线程并在滚动时多次重新下载同一图像,使用可获得单一异步下载和缓存的好处

import SDWebImage 
cell.paymentImage.sd_setImage(with: URL(string:aData.iconHome ?? ""), placeholderImage: UIImage(named: "placeholder.png"))

谢谢,我已经安装了pod“SDWebImage”cocopods,只是有一个疑问,我从json获得了所有图像url,但在集合视图中,我没有获得前10个,其余的都来了为什么..有什么想法吗确保url不是零