如何在Swift中从图像数组中释放图像 [英] How to Release Images from an Image Array in Swift

查看:127
本文介绍了如何在Swift中从图像数组中释放图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有'核选项'从图像阵列中释放图像?我想播放第一个animation1(ImageSet1),然后在完成块中删除这个动画,然后加载并播放第二个animation2(ImageSet2),依此类推:清洗,冲洗,重复 - 你得到它:)

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 :)

首先,我定义了我的ViewController的实例变量:

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
    }

然后我设置了animate值:

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")

最后调用animate函数

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

}) 
}

完成后,我将animation1数组设置为等于[],它确实删除了动画(你不能再看到它)但内存占用量仍然完全相同(我也尝试了其他三个选项)。

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).

有了上述内容,每次添加下一个动画时内存占用量都会上升,最终导致操作系统终止App

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

即使更改ViewControllers,数组仍然将图像保存在内存中

Even when you change ViewControllers, the array is still holding the images in memory

感谢您的帮助:))

推荐答案

UIImage(命名:)将始终将图像保存在内存堆栈中,除非需要更多内存和足够的时间来解除分配。

UIImage(named: ) will always save the image in the memory stack unless there is a requirement for more memory and enough time to deallocate.

为了释放我来自内存的mages你应该使用其他构造函数:UIImage(contentsOfFile :)并将图像保存在文件夹而不是资源管理器中。也许数据也可以正常工作,但我不确定。

In order to release images from memory you should use the other constructor: UIImage(contentsOfFile: ) and save the images in folder instead of the Asset manager. Maybe data will work as well but i'm not sure.

临时/大资源会消耗堆,你应该使用诸如contentsOfFile之类的临时保存方法来清除它来自记忆。

Temporary / Large resources will consume the heap, you should use "temp saving" methods such as contentsOfFile to clear it from the memory.

这篇关于如何在Swift中从图像数组中释放图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆