OGeek|极客世界-中国程序员成长平台

标题: ios - 绘制多个 UIImage 时如何优化 CGContext 内存消耗? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 18:18
标题: ios - 绘制多个 UIImage 时如何优化 CGContext 内存消耗?

我编写了一个函数来帮助将 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 中,但仍然没有运气...

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



Best Answer-推荐答案


尝试在 for 循环中调用 UIGraphicsBeginImageContextWithOptions(size, false, 0.0)UIGraphicsEndImageContext()。它将限制 session 中的数据处理量,即在一个上下文中只处理一张图像。

关于ios - 绘制多个 UIImage 时如何优化 CGContext 内存消耗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45159363/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4