使用UIImageView.animationImages和startAnimating()时,我的代码中的内存可以更好地管理吗? [英] Can memory in my code be better managed when using UIImageView.animationImages and startAnimating()?

查看:198
本文介绍了使用UIImageView.animationImages和startAnimating()时,我的代码中的内存可以更好地管理吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在声明一些UIImage数组:

I'm declaring some UIImage arrays:

var animationImages1: [UIImage] = []
var animationImages2: [UIImage] = []

我正在使用后台线程加载图片:

I'm using a background thread to load the images:

DispatchQueue.global(qos: .background).async { () -> Void in

self.animationImages1 = self.createImageArray(total: 57, imagePrefix: "animation1")
self.animationImages2 = self.createImageArray(total: 42, imagePrefix: "animation2")

}

上面调用的函数:

var imageArray: [UIImage] = []

for imageCount in 1..<total {
     var imageName = String(format: "\(imagePrefix)\(imageCount)")

     let image = UIImage(contentsOfFile: Bundle.main.path(forResource: imageName, ofType: "png")!)!
     //let image = UIImage(named: imageName)!

     imageArray.append(image)
}

return imageArray

然后当我想要动画时,我正在调用此函数:

Then when I want to animate, I'm calling this function:

func animate(imageView: UIImageView, images: [UIImage], duration: TimeInterval) {
     imageView.animationImages = images
     imageView.animationDuration = duration
     imageView.animationRepeatCount = 1
     imageView.startAnimating()
}

我试过做两个 theImageView.stopAnimating() theImageView.animationImages = nil 再次调用动画之前,但没有注意到使用任何内存管理的任何改进。

I tried doing both theImageView.stopAnimating() and theImageView.animationImages = nil before calling the animation again but didn't notice any improvement to memory management using either.

使用UIImage(命名为:),应用程序中可见的图像在内存不足时开始部分或完全消失。使用UIImage(contentsOfFile :),一旦内存不足,应用程序会立即崩溃。

With UIImage(named:) the images visible in the app start disappearing either partially or completely as memory runs low. With UIImage(contentsOfFile:) the app promptly crashes once memory runs low.

注意:我尝试使用资产目录中的图像UIImage(名为:),然后切换到UIImage(contentsOfFile :),将图像拖入Assets.xcassets外的项目

To note: I tried UIImage(named:) with the images in the Assets catalog, and then switched to UIImage(contentsOfFile:) with the images dragged in to the project outside of Assets.xcassets

是否可以使用UIImageView的这个功能来制作更长的动画(2- 5秒:40-150 pngs,每个文件大小约450k,或者无论你如何去做它都会造成太大的记忆力?

Is it possible to use this function of UIImageView for longer animations (2-5 seconds: 40-150 pngs) with a file size of about 450k each, or is it too much a memory strain regardless of how you go about it?

目前在较新的iPad Pro上运行没有问题,并且使用Xcode的模拟器,它在所有设备尺寸上运行良好(但占用大量内存)。在iPhone X和iPhone 8 Plus上,它很早就耗尽了内存 - 在播放了5到10个动画之后。

It currently runs without an issue on a newer iPad Pro, and using Xcode's simulator it runs well (but eats a lot of memory) on all device sizes. On an iPhone X and an iPhone 8 Plus, it runs out of memory pretty early on - after playing through 5 to 10 animations or so.

我错过了什么,是它不可能,或者我是否需要进一步研究如何通过startAnimating()运行这些大型UIImage数组来控制内存?

Am I missing something, is it not possible, or do I need to do further research on ways to keep memory in check while running these large UIImage arrays through startAnimating()?

内存使用情况不会发生下。我必须在某个地方缓存...

Memory usage is not going down. I must be caching this somewhere...

感谢您的帮助!

推荐答案

我创建了一个简化的新Xcode项目,专注于这个问题,并且@Stephan Schlecht在这里给出了一个正确答案

I created a new Xcode project simplified to focus just on this issue, and was given a correct answer by @Stephan Schlecht here.

虽然在将图像分配给数组时没有发生内存命中,并且仅在播放动画时才会发生内存命中,但仍然是唯一的回收内存的方法似乎是通过将UIImageView上的变量数组和animationImages属性设置为空白数组来删除对图像数组的所有引用。

Although the memory hit doesn't occur when assigning the images to an array, and the memory hit only happens once the animation is played, still the only way to reclaim the memory seems to be removing all references to the image array by setting both the variable array and the animationImages property on the UIImageView to a blank array.

所以在这个问题给出的具体例子:

So in the particular example given in this question:

animationImages1 = []
imageView.animationImages = []

这篇关于使用UIImageView.animationImages和startAnimating()时,我的代码中的内存可以更好地管理吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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