gpt4 book ai didi

swift - 如何异步加载图像以提高 TableView 性能?

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

我的图片有问题。我的表格 View 很慢。如何通过异步调用进行改进?在创建单元格的函数中,我有这个:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//IMPOSTA l'identifier della cella campione indexpath restituisce il numero della cella
let cella = tableView.dequeueReusableCellWithIdentifier("record_cat", forIndexPath: indexPath) as! myTableViewCell

if (musica.shared.id.count > 0) && (cella_select < musica.shared.id.count) {
//label personalizzata cella con inserimento dell ARRAY IN BASE ALL'INDICE (indexPath.row indica il numero della cella quindi associa cella ad array)
print(indexPath.row)

cella.button_favorite.tag = indexPath.row
cella.titolo.text = musica.shared.titles[indexPath.row]
cella.artista.text = musica.shared.artists[indexPath.row]
cella.logo_new.hidden = true

let url_img = NSURL(string: "https://tuunes.co/app/upload/\(musica.shared.images[indexPath.row])")
let data_img = NSData(contentsOfURL: url_img!)

if (data_img != nil) {
cella.immagine.image = UIImage(data: data_img!)
} else {
cella.immagine.image = UIImage(named: "cover-img-list")
}

} else {
return cella
}

return cella
}

最佳答案

只需安装 SDWebImage通过CocoaPods

如何使用

导入SDWebImage在你的ViewController您的 table 在哪里:

import SDWebImage

然后将此代码替换为 cellForRowAtIndexPath

let data_img = NSData(contentsOfURL: url_img!)
if (data_img != nil){
cella.immagine.image = UIImage(data: data_img!)
} else {
cella.immagine.image = UIImage(named: "cover-img-list")
}

cella.immagine.sd_setImageWithURL(url_img!, placeholderImage: UIImage(named: "cover-img-list"))

关于swift - 如何异步加载图像以提高 TableView 性能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40049221/

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