动画UIImageView

时间:2018-01-07 17:36:08

标签: ios swift animation uiimageview

当我使用图像序列设置动画我的imageView时,内存使用量会增加很多,并且在动画完成后也不会恢复正常。这是我的代码

override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = .black
    view.addSubview(animationImage)
    animationImage.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    animationImage.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
    animationImage.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.9, constant: 0).isActive = true
    animationImage.heightAnchor.constraint(equalToConstant: 400).isActive = true

    var i = 0
    while i < 15 {
        let path = Bundle.main.path(forResource: "findItBaltas\(i)", ofType: "png")
        animationImages.append(UIImage(contentsOfFile: path!)!)
        i += 1
    }
    animationImage.animationImages = animationImages
    animationImage.animationDuration = 3
    animationImage.animationRepeatCount = 1
    animationImage.startAnimating()

}

var animationImages = [UIImage]()

let animationImage: UIImageView = {
   let imageView = UIImageView()
    imageView.translatesAutoresizingMaskIntoConstraints = false
    return imageView
}()

我在谷歌上搜索了一整天,我发现这是因为动画图像被缓存到设备的内存中我应该使用UIImage(contentsOfFile :)而不是UIImage(命名:)但问题仍然存在记忆力不会恢复正常。有哪些其他选项可以播放动画一次并从内存中删除它的缓存?

1 个答案:

答案 0 :(得分:1)

您对UIImage(contentsOfFile:)的使用很好,但您将图像存储在数组属性中而不是区域设置变量中。因此,您的应用程序可长期强有力地参考所有图像。

移动:

var animationImages = [UIImage]()

viewDidLoad方法内部,因此它是局部变量而不是类属性。

但即便如此,图像视图仍将所有图像保存在内存中。在完成动画制作时,您需要添加代码以将图片视图animationImages设置为nil。如果您希望将其保留在视图中,也许您可​​以将其image属性设置为最后一个图像。

相关问题