菜鸟教程小白 发表于 2022-12-11 18:18:39

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


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

<pre><code>UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
let context = UIGraphicsGetCurrentContext()

for index in 0..&lt;numPrintsPerTemplate {

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

let image = images
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
</code></pre>

<p>对于少量的 UIImage 可以正常工作,但是由于高内存消耗而不断崩溃。我试图将这段代码包装在 <code>autoreleasepool {}</code> 中,但没有成功。我尝试的另一种尝试是将 <code>autoreleasepool {}</code> 放在 <code>for-loop</code> 中,但仍然没有运气...</p>

<p>以前有人遇到过这个问题吗?我错过了什么明显的东西吗?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>尝试在 for 循环中调用 <code>UIGraphicsBeginImageContextWithOptions(size, false, 0.0)</code> 和 <code>UIGraphicsEndImageContext()</code>。它将限制 session 中的数据处理量,即在一个上下文中只处理一张图像。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 绘制多个 UIImage 时如何优化 CGContext 内存消耗?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/45159363/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/45159363/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 绘制多个 UIImage 时如何优化 CGContext 内存消耗?