我的图片有问题。我的表视图很慢。如何通过异步调用改进?在创建细胞的过程中,我有这个:
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
}
答案 0 :(得分:0)
只需通过CocoaPods
如何使用
在您的表所在的SDWebImage
中导入ViewController
:
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"))
答案 1 :(得分:0)
//Set placeholder sync
cella.immagine.image = UIImage(named: "cover-img-list")
if let urlImg = NSURL(string: "https://tuunes.co/app/upload/\(musica.shared.images[indexPath.row])") {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
//load data on the background thread
if let dataImg = NSData(contentsOfURL: urlImg) {
dispatch_async(dispatch_get_main_queue()) {
//Update UI on the main thread
cella.immagine.image = UIImage(data: dataImg)
}
}
}
}