Swift - 如果图像与之前的图像相同,如何停止重新加载图像?

时间:2016-06-03 00:16:32

标签: ios image uitableview

我有一个应用程序运行一堆json API请求到服务器,然后加载信息和图像以及该信息。问题是,当我向下移动一个表然后再次备份时,它会使用我设置的动画重新加载图像(alpha = 0到1)并且我不想要它,因为它看起来很糟糕。我尝试告诉服务器是否newImage == cell.imageview.image,但当然不能正常工作,因为它与内存中的图像不同。我该如何解决这个问题?

我能想到的唯一方法是子类化或扩展UIImage类并添加一个字符串作为图像的链接,然后当我设置图像时,我也检查是否链接是一样的。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您可以保留已完成动画的字典/一组indexPath。因此,在添加动画之前检查字典中的indexPath,并在动画完成时添加indexPath。你的代码看起来像这样:

    //in your viewController class
    var animateIndexPaths = Set<NSIndexPath>()

    //tableview delegate
    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        if !animateIndexPaths.contains(indexPath) {
            UIView.animateWithDuration(0.5, animations: { 
                    //do your animation
                }, completion: { (stop:Bool) in
                    self.animateIndexPaths.insert(indexPath)
            })
        }
    }