如何在绘制多个UIImage时优化CGContext内存消耗?

时间:2017-07-18 06:42:34

标签: ios swift uiimage

我编写了一个函数来帮助将UIImage数组绘制到单个UIImage上,并在Swift中使用以下代码块:

UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
let context = UIGraphicsGetCurrentContext()

for index in 0..<numPrintsPerTemplate {

  // .... update x,y positions for the image ...

  let image = images[index]
  let imageRect = CGRect(x: x, y: y, width: imageWidth, height: imageHeight)
  image.draw(in: imageRect, blendMode: .normal, alpha: 1)

}

let template = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return template

它适用于少量UIImage,但由于高内存消耗而继续崩溃。我试图在autoreleasepool {}中包装这段代码,但没有成功。我尝试过的另一种尝试是将autoreleasepool {}放在for-loop内,但仍然没有运气......

之前有人遇到过这个问题吗?我错过了什么明显的东西吗?

1 个答案:

答案 0 :(得分:0)

尝试将UIGraphicsBeginImageContextWithOptions(size, false, 0.0)UIGraphicsEndImageContext()的来电置于for循环中。它将限制会话中的数据处理量,即在一个上下文中只处理一个图像。

相关问题