gpt4 book ai didi

calayer - 将 CALayer 绘制到 CGContext 中

转载 作者:行者123 更新时间:2023-12-04 06:47:15 30 4
gpt4 key购买 nike

我有以下代码:

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSaveGState(context);

CALayer *sublayer = [CALayer layer];
sublayer.backgroundColor = [UIColor orangeColor].CGColor;
sublayer.cornerRadius = 20.0;
sublayer.frame = CGRectMake(20, 0, 300, 20);

[sublayer setNeedsDisplay];
[sublayer drawInContext:context];

UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return newImage;

但是当我查看返回的 newImage 时,只有一个空图像。当我将 drawInContext 更改为 renderInContext 时,我得到了上面的子层,但似乎坐标系困惑了。

知道为什么上面的 drawInContext 不起作用吗?

最佳答案

尝试使用 renderInContext代替 drawInContext .

我的理解是drawInContext有没有被覆盖,而 renderInContext用于将图层的内容渲染到上下文中。

来自 the documentation :

- drawInContext:

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.

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.



- renderInContext:

This method renders directly from the layer tree, ignoring any animations added to the render tree. Renders in the coordinate space of the layer.

关于calayer - 将 CALayer 绘制到 CGContext 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6133509/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com