菜鸟教程小白 发表于 2022-12-13 06:51:25

ios - view.layer drawInContext :UIGraphicsGetCurrentContext() not drawing


                                            <p><p>我正在尝试将 <code>UIView</code> 绘制到 <code>UIImage</code> 中。这是我正在使用的代码:</p>

<pre><code>UIGraphicsBeginImageContextWithOptions(myView.bounds.size, YES, 0.f);
;
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
</code></pre>

<p>我已验证 <code>myView.bounds.size</code> 有效。 <code>myView</code> 在屏幕上正确显示。但是,<code>img</code> 完全是黑色的(我尝试在 <code>UIImageView</code> 中同时显示并尝试将 JPEG 表示写入文件。)图像尺寸正确,色彩空间(在 JPEG 文件输出中)是 RGB,颜色配置文件是 sRGB 等,这意味着我们不是在处理损坏的图像(在图像/位图数据本身有效的意义上)。我已经在 6.0 模拟器、7.0 模拟器和 7.0.6 设备上测试过这个案例,都一样。该图层没有任何子图层,我尝试将 <code>masksToBounds</code> 设置为 <code>NO</code> ,但没有任何改变。</p>

<p>什么可能导致 View 层不绘制? </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>你需要改变:</p>

<p><code>;</code></p>

<p>到:</p>

<p><code>;</code></p>

<p>请注意,<code>drawInContext:</code> 默认情况下实际上并没有做任何事情:</p>

<blockquote>
<p>The default implementation of this method does not doing any drawing
itself. If the layer’s delegate implements the drawLayer:inContext:
method, that method is called to do the actual drawing.</p>

<p>Subclasses can override this method and use it to draw the layer’s
content. When drawing, all coordinates should be specified in points
in the logical coordinate space.</p>
</blockquote>

<p>UIView 的层委托(delegate)被设置为 UIView,但它看起来并不像 UIView 必须绘制到提供的上下文。需要对此进行更多调查。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - view.layer drawInContext :UIGraphicsGetCurrentContext() not drawing,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/22286863/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/22286863/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - view.layer drawInContext :UIGraphicsGetCurrentContext() not drawing