Is there a 'nuclear option' to release the images from the image array? I would like to play the first animation1 (ImageSet1), then in the completion block delete this animation and then load and play the second animation2 (ImageSet2), and so on: wash, rinse, repeat - you get it :)
First, I define the instance variables of my ViewController:
var animation1: [UIImage] = []
var animation2: [UIImage] = []
var animation3: [UIImage] = []
Then I create an Animation Array for each animation:
func createImageArray(total: Int, imagePrefix: String) -> [UIImage]{
var imageArray: [UIImage] = []
for imageCount in 0..<total {
let imageName = "\(imagePrefix)-\(imageCount).png"
let image = UIImage(named: imageName)!
imageArray.append(image)
}
return imageArray
}
Then I set the animate values:
func animate(imageView: UIImageView, images: [UIImage]){
imageView.animationImages = images
imageView.animationDuration = 1.5
imageView.animationRepeatCount = 1
imageView.startAnimating() }
animation1 = createImageArray(total: 28, imagePrefix: "ImageSet1")
animation2 = createImageArray(total: 53, imagePrefix: "ImageSet2")
animation3 = createImageArray(total: 25, imagePrefix: "ImageSet3")
And lastly call the animate function
func showAnimation() {
UIView.animate(withDuration: 1, animations: {
animate(imageView: self.animationView, images: self.animation1)
}, completion: { (true) in
// Release the Images from the Array
})
}
I would like to release the images from the image array in the completion block to lower my memory footprint, but I can't seem to get this to work, I have tried all four examples below but there is no effect on the memory footprint:
func showAnimation() {
UIView.animate(withDuration: 1, animations: {
animate(imageView: self.animationView, images: self.animation1)
}, completion: { (true) in
self.animation1 = [] // is this correct?
self.animationView.image = nil // also doesn't work
self.animationView.removeFromSuperview() // also doesn't work
self.animation1 = nil // also doesn't work
})
}
After the completion block, I set the animation1 array equal to [] and it does remove the animation (you can't see it any more) but the memory footprint remains the exact same (I have also tried the other three options as well).
With the above, the memory footprint 'bumps' up each time I add the next animation and it eventually causes the OS to terminate the App
Even when you change ViewControllers, the array is still holding the images in memory
Thanks for any help :)