【问题标题】:UIImageView memory increases when changing image first time第一次更改图像时 UIImageView 内存增加
【发布时间】:2018-09-03 05:36:21
【问题描述】:

一个演示项目 -

我有一个 UIImageView 和三个按钮。

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

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

这是 UIImage 视图的常见行为吗? 实际上,我正在处理相框,每次应用新的帧内存都会增加。因此,在应用 30-40 帧后应用程序崩溃。

有人可以参考如何处理此类相框应用程序或 UIImagesView 占用内存的任何原因吗?

提前致谢!

【问题讨论】:

    标签: memory-management swift3 uiimageview image-size


    【解决方案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

    【讨论】:

    • 非常感谢您的大力帮助 :)
    猜你喜欢
    • 2012-07-22
    • 2013-08-09
    • 1970-01-01
    • 2018-06-21
    • 1970-01-01
    • 1970-01-01
    • 2013-04-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多