如何异步动态UITableView的负荷图像? [英] How to async load images on dynamic UITableView?
本文介绍了如何异步动态UITableView的负荷图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
现在我与图像和标签自定义单元格。后来我通过打印这些细胞:
Now I have a custom cell with the image and label. Later I print these cells by:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:customCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! customCell
var url = NSURL(string: postCover[indexPath.row])
if let data = NSData(contentsOfURL: url!) {
cell.cellImage.image = UIImage(data: data)
}
var title = postTitle[indexPath.row]
cell.cellLabel.text = title
let indexPath = tableView.indexPathForSelectedRow()
return cell
}
和当我打开一个应用程序,并滚动它冻结。我怎样才能修复这个bug?
and when I open an app and scroll it freezes. How can I fix this bug?
我读到它,我必须用异步加载图像在我的tableView,但不要我加载它们异步了?
I read about it, that I must use async image loading in my tableView, but don't I load them async already?
我搜索,想想4-5天并没有什么。谁能给我一些建议,好吗?
I searched and think about it 4-5 days and nothing. Can anyone give me some tips, please?
推荐答案
您可以使用异步调度图像的下载每个单元的GCD AS-
You can schedule the downloading of image asynchronously for each cell using GCD as-
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:customCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! customCell
var url = NSURL(string: postCover[indexPath.row])
cell.imageView.image = nil;
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^(void) {
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:parsedData[@"imageLR"]];
UIImage* image = [[UIImage alloc] initWithData:imageData];
if (image) {
dispatch_async(dispatch_get_main_queue(), ^{
if (cell.tag == indexPath.row) {
cell.imageView.image = image;
[cell setNeedsLayout];
}
});
}
});
return cell
}
有关雨燕,你可以做这样
for Swift you can do so as
if let data = self.cache.objectForKey(urlString) as? NSData {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)) {
let image = UIImage(data: data)
dispatch_async(dispatch_get_main_queue()) {
cell.imageView.image = image;
}
}
这篇关于如何异步动态UITableView的负荷图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文