[英]Animating 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(named :),但問題仍然存在,並且內存無法恢復正常。 播放動畫一次並從內存中刪除其緩存還有哪些其他選擇?
您對UIImage(contentsOfFile:)
的使用很好,但是您將圖像存儲在數組屬性中,而不是語言環境變量中。 因此,您的應用對所有圖像都有長期的強大參考。
移動:
var animationImages = [UIImage]()
放在viewDidLoad
方法內部,因此它是一個局部變量而不是類屬性。
但是即使那樣,圖像視圖仍將所有圖像保留在內存中。 完成動畫animationImages
,您需要添加代碼以將圖像視圖的animationImages
為nil
。 如果您希望將其image
屬性保持在最后,可以將其image
屬性設置為僅最后一個圖像。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.