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

标签 swift uitableview uiimage

我的图片有问题。我的表格 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/

相关文章:

iOS:使用 UIImage 在 collectionview reoloaddata 中崩溃

cocos2d-iphone - 如何将 CCSprite 图像转换为 UIImage?

objective-c - 使用 inputAccessoryView 关闭键盘

ios - 未调用 XMLParser 委托(delegate)方法

swift - 在 Swift 3 中通过 IBInspectable 传递选择器

objective-c - Objective-C : How to make background color of UITableView Consistent

ios - 无法形成对类实例的弱引用

ios - UITableView 中多张图片的延迟加载

ios - 按钮上的 UITableViewCell 单击使用核心数据更新数据库属性值

ios - CGRect 到 UIImage