添加边框到UIImages内存问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了添加边框到UIImages内存问题相关的知识,希望对你有一定的参考价值。

我试图通过在上下文中绘制边框来为我的图像添加边框,但内存不断增长。

func borderImages(image: [UIImage]) -> [UIImage] {
    let width: CGFloat = 3000
    let height: CGFloat = 3000
    var borderedImages = [UIImage]()

    for image in images {
        autoreleasepool {
            UIGraphicsBeginImageContextWithOptions(CGSize(width: width, height: height), true, 1.0)

            let x: CGFloat = (width - image.size.width)/2.0
            let y: CGFloat = (height - image.size.height)/2.0

            let rect = CGRect(x: x, y: y, width: image.size.width, height: image.size.height)
            image.draw(in: rect)

            if let borderedImage = UIGraphicsGetImageFromCurrentImageContext() {
                borderedImages.append(borderedImage)

            }
            UIGraphicsEndImageContext()
        }
    }

    return borderedImages
}

我尝试按照here的建议添加autoreleasepool。似乎没有任何区别。这是分配在崩溃之前的样子.enter image description here关于如何解决这个问题的任何建议?我使用autoreleasepool错了吗?

答案

因此,在讨论它并检查图像后,它看起来像是尝试立即处理100,这对于ios设备的内存处理来说太多了。

解决方案是批处理文件以进行如下处理:

  1. 创建下一批。
  2. 处理/上传当前批次中的图像。
  3. 处理当前批次。
  4. 重复上面的1。

这样做你永远不应该在内存中保留太多内容。

以上是关于添加边框到UIImages内存问题的主要内容,如果未能解决你的问题,请参考以下文章

如何删除画布周围的灰色边框

将多个 UIWebViews 作为 UIImages 写入 PDF 文件

iPhone 应用程序内存泄漏与 NSMutableArray 中的 UIImages

如何在运行时正确压缩 UIImages

读取视频帧 iPhone 时出现内存问题

如何在 xCode 中保存 UIimages 数组(swift)