在后台加载gif图片?

时间:2015-04-13 04:12:00

标签: xcode swift asynchronous gif

说明/我尝试过的内容

我导入了一个库,可以让我以异步方式将图像加载到我的UITableView(https://github.com/natelyman/SwiftImageLoader)中。问题是它不支持GIF。所以我做了case语句来独立检索gif(如果需要)并使用另一个库显示gif。

问题

现在问题是每当我尝试加载gif时,滚动变得非常不稳定。

问题

有人可以帮我解决这个问题吗?它是否涉及将它放在后台线程上?如果是这样,怎么样?

代码

if let url = NSURL(string: arrayByVotes[indexPath.row].objectForKey("thumbnail") as NSString) {

        let imageURL = arrayByVotes[indexPath.row].objectForKey("thumbnail") as NSString

        if (imageURL.lowercaseString.rangeOfString("gif") != nil) {

                    cell.thumbnail.image = UIImage.animatedImageWithAnimatedGIFURL(url)


        } else {

            ImageLoader.sharedLoader.imageForUrl(arrayByVotes[indexPath.row].objectForKey("thumbnail") as NSString, completionHandler:{(image: UIImage?, url: String) in

                cell.thumbnail.image = image
            })

        }

    }

1 个答案:

答案 0 :(得分:-1)

也许在内存中同时存在许多gif图像会影响滚动的性能。尝试从不可见的单元格中删除图像,并仅在单元格显示时添加图像。一个很好的方法是来自UITableViewDelegate的tableView(_:willDisplayCell:forRowAtIndexPath :)