gpt4 book ai didi

ios - 如何在 Swift 中从图像数组中释放图像

转载 作者:搜寻专家 更新时间:2023-10-31 23:08:19 37 4
gpt4 key购买 nike

是否有从图像阵列中释放图像的“核选项”?我想播放第一个 animation1 (ImageSet1),然后在完成 block 中删除此动画,然后加载并播放第二个 animation2 (ImageSet2),依此类推:清洗、漂洗、重复 - 你明白了 :)

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

var animation1: [UIImage] = []
var animation2: [UIImage] = []
var animation3: [UIImage] = []

然后我为每个动画创建一个动画数组:

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
}

然后我设置动画值:

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

最后调用动画函数

func showAnimation() {
UIView.animate(withDuration: 1, animations: {
animate(imageView: self.animationView, images: self.animation1)

}, completion: { (true) in

// Release the Images from the Array

})
}

我想从完成 block 中的图像数组中释放图像以减少我的内存占用,但我似乎无法让它工作,我已经尝试了下面的所有四个示例,但对内存占用:

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

})
}

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

有了上面的内容,每次我添加下一个动画时,内存占用都会“增加”,最终导致操作系统终止应用程序

即使您更改了 ViewController,该数组仍将图像保存在内存中

感谢您的帮助:)

最佳答案

UIImage(named: ) 将始终将图像保存在内存堆栈中,除非需要更多内存和足够的时间来释放。

为了从内存中释放图像,您应该使用另一个构造函数:UIImage(contentsOfFile:) 并将图像保存在文件夹中而不是资源管理器中。也许数据也会起作用,但我不确定。

临时/大型资源会消耗堆,您应该使用“临时保存”方法(例如 contentsOfFile)将其从内存中清除。

关于ios - 如何在 Swift 中从图像数组中释放图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47735975/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com