第一次更改图像时,UIImageView内存会增加

问题描述 投票:0回答:1

演示项目 -

我有一个UIImageView和三个按钮。

每当我点击任何按钮时,我都会更改图像视图的图像。我的图片大小为1242 * 1242。

问题:当我点击按钮时,每次首先占用内存5 MB。例如,我点击了按钮1,内存增加了5 MB然后我按了按钮2,内存增加了5 MB。但是,当我再次单击按钮1时,没有内存增加。

这是UIImage View的常见行为吗?实际上,我正在研究相框,每次我应用新的帧存储器时都会增加。因此应用30-40帧应用程序崩溃后。

有人可以参考如何使用这种类型的相框应用程序或任何理由为什么UIImagesView记忆?

提前致谢!

memory-management swift3 uiimageview image-size
1个回答
1
投票

是的@Amanpreet,我也有同样的行为。在调查仪器时,我发现每次在按钮点击时加载新图像时,会分配一些内存作为ImageIO-jpeg-data。

- - - - - - - - - - -解 - - - - - - - - - - - - - - -

用imageView加载图像

UIImage(contentsOfFile: filePath!)

方法。现在,每次使用新图像时,您的内存大小都不会增加。

示例代码

@IBAction func loadOne(_ sender: UIButton) {
    let filePath = Bundle.main.path(forResource: "1", ofType: "jpg")
    imageView.image = UIImage(contentsOfFile: filePath!)
}

@IBAction func loadTwo(_ sender: UIButton) {
    let filePath = Bundle.main.path(forResource: "2", ofType: "jpg")
    imageView.image = UIImage(contentsOfFile: filePath!)

}

@IBAction func loadThree(_ sender: UIButton) {
    let filePath = Bundle.main.path(forResource: "3", ofType: "jpg")
    imageView.image = UIImage(contentsOfFile: filePath!)
}

我正在使用大小为1920 * 1280的图像,以前我的内存使用量增加了如41.6 MB,51.5 MB,59.5 MB,67.5 MB,每次点击后,但采用上面的解决方案后,内存使用量达到50.4 MB

© www.soinside.com 2019 - 2024. All rights reserved.