Ios Swift Json Tableview laggy

Ios Swift Json Tableview laggy,ios,json,swift,uitableview,Ios,Json,Swift,Uitableview,我有个问题。我在Swift中有此代码,但当我在tableview中滚动时,它非常滞后,我不知道问题出在哪里。。?? 它下载的图像数据每个都有20mb(我想) 谢谢 func downloadJsonWithTask() { let url = NSURL(string: urlString) var downloadTask = URLRequest(url: (url as? URL)!, cachePolicy: URLRequest.CachePolicy.reload

我有个问题。我在Swift中有此代码,但当我在tableview中滚动时,它非常滞后,我不知道问题出在哪里。。?? 它下载的图像数据每个都有20mb(我想)

谢谢

func downloadJsonWithTask() {

    let url = NSURL(string: urlString)

    var downloadTask = URLRequest(url: (url as? URL)!, cachePolicy: URLRequest.CachePolicy.reloadIgnoringCacheData, timeoutInterval: 200)

    downloadTask.httpMethod = "GET"

    URLSession.shared.dataTask(with: downloadTask, completionHandler: {(data, response, error) -> Void in

        let jsonData = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments)

        print(jsonData)

    }).resume()
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TableViewCell
    cell.nameLabel.text = nameArray[indexPath.row]
    cell.dobLabel.text = dobArray[indexPath.row]

    let imgURL = NSURL(string: imgURLArray[indexPath.row])

    if imgURL != nil {
        let data = NSData(contentsOf: (imgURL as? URL)!)
        cell.imgView.image = UIImage(data: data as! Data)
    }

    return cell
}

///for showing next detailed screen with the downloaded info
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let vc = self.storyboard?.instantiateViewController(withIdentifier: "DetailViewController") as! DetailViewController
    vc.imageString = imgURLArray[indexPath.row]
    vc.imageString2 = imgURL2Array[indexPath.row]
    vc.imageString3 = imgURL3Array[indexPath.row]
    vc.imageString4 = imgURL4Array[indexPath.row]
    vc.imageString5 = imgURL5Array[indexPath.row]
    vc.imageString6 = imgURL6Array[indexPath.row]
    vc.nameString = nameArray[indexPath.row]
    vc.dobString = dobArray[indexPath.row]
    vc.txtString = txtArray[indexPath.row]
    vc.contactString = contactArray[indexPath.row]

    self.navigationController?.pushViewController(vc, animated: true)
}

这个问题是因为这条线

 let data = NSData(contentsOf: (imgURL as? URL)!)
它会阻塞主线程,您必须在其他队列中运行它,在它旁边,每次滚动(没有缓存)都会重新下载图像,所以最好使用它

这将永远滞后。 在主线程中请求图像

尝试使用SDWebCache库:

1:它将初始化映像并避免多个HTTP请求

2:在后台工作,因此不会影响主线程


使用方法
imageview.sd\u imageFromUrl(URL(..)!

非常清楚。您可以从主线程请求图像数据。每当您在主线程上执行耗时的操作时,应用程序都会延迟

let data = NSData(contentsOf: (imgURL as? URL)!)
到目前为止,对我有效的最佳解决方案是使用AlamofireImage或翠鸟。这需要您了解如何将库添加到项目中。为了方便起见,尽量使用椰子荚

我想你可以在这里用椰子荚

如果您想获得缓存支持,翠鸟优先。缓存意味着您将只需要在第一次或某些内容发生更改时向服务器发出请求。它将节省您的资源等。您所需要做的就是在ViewController中导入翠鸟模块,然后它将使用翠鸟扩展自动扩展UIImageView功能
kf

下面是示例代码

//ViewController.swift
import Kingfisher

// Your cellForRowAt
...
if let url = URL(string: imgURLArray[indexPath.row]) {
    cell.imgView.kf.setImage(with: url)
}
...
翠鸟将在引擎盖下执行缓存策略,以便您可以专注于应用程序

为了放置默认图像,它将非常有用地通知用户有关图像的信息,(用户希望在空白的白色框中有一些东西——UIImageView,而图像为零——在他们的屏幕中不是吗?)

let defaultImage = UIImage(named: "default_img")!
cell.imgView.kf.setImage(with: url, placeholder: defaultImage)

在这里获取有关翠鸟的完整信息

可能与20mb的每侧注释有关-不要在Swift中使用
NSURL
NSData
。而且永远不要使用
NSData(contentsOf:)
加载远程数据。@rmaddy谢谢!我对此一无所知,但我应该用什么来代替呢?您必须始终异步加载远程数据。通常,这是通过URLSession完成的。但对于带有缓存的远程表视图图像,SDWebImage似乎是一个流行的实用程序;这段对话已经结束了,非常感谢。我以前使用过alamofire,但后来我切换到json文件来存储tableview中的所有数据。我要去测试翠鸟或者阿拉莫菲图像,因为正如你说的,我需要缓存所有的图像。我会尝试,然后更新你:)非常好。缓存将适用于图像等大数据。
let defaultImage = UIImage(named: "default_img")!
cell.imgView.kf.setImage(with: url, placeholder: defaultImage)