如何从UIView创建一个CGLayer用于离屏绘图 [英] How to create a CGLayer from a UIView for off-screen drawing

查看:135
本文介绍了如何从UIView创建一个CGLayer用于离屏绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了我认为是Quartz 2D Programming Guide的相关部分,但无法找到以下答案(他们似乎没有在文档中谈论过很多关于iOS的内容):

I have read what I believe to be the relevant parts of the Quartz 2D Programming Guide, but cannot find an answer to the following (they don't seem to talk a lot about iOS in the document):

我的应用程序在 UIView 中显示图形。我不时地以某种方式更新图纸,例如更改其中一个形状的填充颜色(我将 CGPathRefs 保留到重要的形状,以便以后可以使用不同的填充颜色重绘它们)。如上述文档第169页的使用CGLayer绘图一节中所述,我正在考虑将整个绘图绘制到 CGContext 中,我将从 CGLayer ,如下所示:

My application displays a drawing in a UIView. Every now and then I have to update the drawing in some way, e.g. change the fill colour of one of the shapes (I keep CGPathRefs to the important shapes to be able to redraw them with a different fill colour later). As described in the Section "Drawing With a CGLayer" on page 169 of the aforementioned document, I was thinking of drawing the entire drawing into a CGContext that I would obtain from a CGLayer, like so:

CGContextRef offscreenContext = CGLayerGetContext(offscreenLayer);

然后我可以在屏幕外更新 CGContext 并在 UIView 的drawRect:方法中将 CGLayer 绘制到我的UIView中,如下所示:

Then I could do my updating off-screen into the CGContext and draw the CGLayer into my UIView in the UIView's drawRect: method, like so:

CGContextDrawLayerAtPoint(viewContext, CGPointZero, offscreenLayer);

我遇到的问题是,我从哪里得到 CGLayer 来自?我的理解是我必须使用 CGLayerCreateWithContext 来提供它并提供一个 CGContext 作为参数,从中继承了它的大部分内容属性。显然,正确的上下文将是 UIView 的上下文,我得到的是

The problem I am having is, where do I get my CGLayer from? My understanding is I have to make it using CGLayerCreateWithContext and supply a CGContext as a parameter from which it inherits most of it's properties. Obviously, the right context would be the context of the UIView, that I am getting with

CGContextRef viewContext = UIGraphicsGetCurrentContext();

但如果我没记错的话,我只能在 drawRect中得到它:方法并且假设我下次给出的上下文在下次调用该方法时将是相同的一个是无效的,即我只能使用 CGContext 在方法中本地。

but if I am not mistaken, I can only get that within the drawRect: method and it is not valid to assume that the context I am given there will be the same one next time the method is called, i.e. I can only use that CGContext locally within the method.

那么,我怎样才能得到一个我可以使用的 CGContext 初始化我的 CGLayer 来创建一个offscreen CGContext 来绘制然后将整个图层绘制回我的 UIView 's CGContext

So, how can I get a CGContext that I can use to initialise my CGLayer to create an offscreen CGContext to draw into and then draw the entire layer back into my UIView's CGContext?

PS:当你在它;如果上面的任何内容没有意义或没有理智,请告诉我。我刚刚开始了解Quartz 2D。

PS: While you're at it; if anything above does not make sense or is not sane, please let me know. I am just starting to get my head around Quartz 2D.

推荐答案

首先,如果你是在iOS上做的话环境,我认为你是对的。文档清楚地说,获得 CGContextRef 的唯一方法是

First of all, if you are doing it from in an iOS environment, I think you are right. The documentation clearly said that the only way to obtain a CGContextRef is by

CGContextRef ctx = UIGraphicGetCurrentContext();

然后使用该上下文创建 CGLayer with

Then you use that context for creating the CGLayer with

CGLayerRef layer = CGLayerCreateWithContext(ctx, (CGSize){0,0}, NULL);

如果你想在那个图层上绘图,你必须用你得到的上下文绘制它这层。 (它与您之前传递的用于创建CGLayer的上下文略有不同)。我猜测 CGLayerCreateWithContext 保存了它可以从传入的上下文中获取的信息,但不是所有内容。 (其中一个示例是ColorSpace信息,您必须在使用 CGLayer 中的上下文填充内容时重新指定。)

And if you want to draw on that layer, you have to draw it with the context you get from the layer. (It is somewhat different from the context you passed in earlier to create the CGLayer). Im guessing the CGLayerCreateWithContext saves the information it can get from the context passed in, but not everything. (One of the example is the ColorSpace information, you have to re-specify when you fill something with the context from CGLayer).

您可以从 CGLayerGetContext()函数中获取CGLayer上下文引用,并使用它来绘制。

You can get the CGLayer context reference from the CGLayerGetContext() function and use that to draw.

CGContextRef layerCtx = CGLayerGetContext(layer);
CGContextBeginPath(layerCtx);
CGContextMoveToPoint(layerCtx, -10, 10);
CGContextAddLineToPoint(layerCtx, 100, 10);
CGContextAddLineToPoint(layerCtx, 100, 100);
CGContextClosePath(layerCtx);

我发现的一点是当你在屏幕外画一些东西时,它会在屏幕外自动剪辑。 (有意义,因此它不会绘制未见的东西)但是当你移动图层时(使用矩阵变换)。修剪后的路径没有显示(缺失)。

One point that I found out is when you draw something offscreen, it automatically clips the thing offscreen. (make sense, so it doesnt draw things that is not seen) but when you move the layer (using the matrix transformation). The clipped path is not showing (missing).

最后一件事,如果将图层的引用保存到变量中,稍后又要绘制它,可以使用 CGContextDrawLayerAtPoint()方法如

One last thing, if you save the reference to a layer into a variable and later on you want to draw it, you can use CGContextDrawLayerAtPoint() method like

CGContextDrawLayerAtPoint(ctx, (CGPoint) {newPointX, newPointY}, layer);

它会在newPointX和新PointY坐标处压印或绘制图层。

It will sort of "stampt" or "draw" the layer at that newPointX and new PointY coordinate.

我希望回答你的问题,如果不是,请告诉我。

I hope that answer your question, if its not please let me know.

这篇关于如何从UIView创建一个CGLayer用于离屏绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆