【问题标题】:Swift cropped UIImage take more than 300 MB MemorySwift 裁剪的 UIImage 占用超过 300 MB 内存
【发布时间】:2021-05-07 09:38:16
【问题描述】:

我有一个包含 96 个 UIImage (1280 * 720) 的数组,我裁剪了每个 UIImage 的 720 * 720,并将新图像保存在一个新数组中。

使用该功能后,我的手机内存增加到 400 MB 并且不会被释放。 我已经尝试使用 autoreleasepool,但它没有帮助。

我的裁剪功能是否有问题或者为什么内存没有被释放?

这是我的代码:

Image of looping through the image array and save the new cropped image

cropping function

Memory

【问题讨论】:

  • UIGraphicsContext 一张图片需要 1280 * 720 * 4 = 3686400 这意味着一张图片需要 3.68 Mb 内存,autoreleasepool 不会释放该内存
  • 您需要以minimal reproducible example 的形式将所有相关代码作为文本包含所有相关代码作为文本,而不是作为屏幕截图,以minimal reproducible example 的形式提出问题主题。

标签: swift memory-leaks uiimage


【解决方案1】:

避免使用UIGraphicsImageBeginWithContext 并使用UIGraphicsImageRenderer,如下所示。

有关更多详细信息,请参阅来自(17:00 分钟):https://developer.apple.com/videos/play/wwdc2018/416

        let bounds = CGRect(x: 0, y: 0, width:720, height: 720)
        let renderer = UIGraphicsImageRenderer(size: bounds.size)
        let image = renderer.image { context in // Use this Image
            // Drawing Code
            Your_View.drawHierarchy(in: bounds, afterScreenUpdates: true)
        }

【讨论】:

    猜你喜欢
    • 2015-06-30
    • 2021-05-21
    • 1970-01-01
    • 2010-09-14
    • 2017-01-11
    • 1970-01-01
    • 1970-01-01
    • 2011-12-07
    • 1970-01-01
    相关资源
    最近更新 更多