我有一个扩展程序,可以在上打印图像URL UIImageView
。但是我认为问题是我tableView
因为这个扩展太慢了。我想我需要为此打开线程。如何在此扩展中创建线程,或者您知道解决该问题的另一种解决方案?
我的代码:
extension UIImageView{
func setImageFromURl(stringImageUrl url: String){
if let url = NSURL(string: url) {
if let data = NSData(contentsOf: url as URL) {
self.image = UIImage(data: data as Data)
}
}
}
}
我认为,这里的问题是,您需要将图像缓存在其中table view
以平滑滚动。每次程序调用时,cellForRowAt indexPath
它都会再次下载图像。这需要时间。
要缓存图像,您可以使用SDWebImage
,Kingfisher等库。
翠鸟用法示例:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "identifier", for: indexPath) as! CustomCell
cell.yourImageView.kf.setImage(with: URL)
// next time, when you will use image with this URL, it will be taken from cache.
//... other code
}
希望能帮助到你
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句