2017-07-18 38 views
0

我写了一个函数来帮助绘制UIImage一个阵列到单个UIImage与雨燕下面的代码块:如何在绘制多个UIImage时优化CGContext内存消耗?

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之内,仍然没有运气...

有人遇到过这个问题吗?我错过任何明显的东西吗?

回答

0

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