问题描述
我已经创建了一个简单的绘图项目,代码工作正常,但我想将绘图缓存到CGlayer中,因为我读到了它在绘图中更有效的方式。我已阅读了这些文件,但无法正确理解。所以朋友们,我请你们在这方面帮助我。
I have created a simple drawing project,the code works fine, but I want to cache the drawing into CGlayer, because I read that its more efficient way in drawing . I have read through the documents, but not able to understand it properly. So friends, I request you to please help me in this regard.
下面是我的代码,我想知道如何在这个中使用CgLayer
Below is my code, I want to know how to use CgLayer in this
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
if(myLayerRef == nil)
{
myLayerRef = CGLayerCreateWithContext(context, self.bounds.size, NULL);
}
CGContextRef layerContext = CGLayerGetContext(myLayerRef);
CGContextDrawLayerAtPoint(context, CGPointZero, myLayerRef);
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
m_previousPoint2 = m_previousPoint1;
m_previousPoint1 = [mytouch previousLocationInView:self];
m_currentPoint = [mytouch locationInView:self];
CGPoint mid1 = midPoint(m_previousPoint1, m_previousPoint2);
CGPoint mid2 = midPoint(m_currentPoint, m_previousPoint1);
testpath = CGPathCreateMutable();
CGPathMoveToPoint(testpath, NULL, mid1.x, mid1.y);
CGPathAddQuadCurveToPoint(testpath, NULL, m_previousPoint1.x, m_previousPoint1.y, mid2.x, mid2.y);
CGContextRef context = UIGraphicsGetCurrentContext();
context = CGLayerGetContext(myLayerRef);
CGRect bounds = CGPathGetBoundingBox(testpath);
CGPathRelease(testpath);
CGRect drawBox = bounds;
//Pad our values so the bounding box respects our line width
drawBox.origin.x -= self.lineWidth * 2;
drawBox.origin.y -= self.lineWidth * 2;
drawBox.size.width += self.lineWidth * 4;
drawBox.size.height += self.lineWidth * 4;
[self setNeedsDisplayInRect:drawBox];
}
- (void) drawingOperations
{
CGContextRef context1 = CGLayerGetContext(myLayerRef);
CGPoint mid1 = midPoint(m_previousPoint1, m_previousPoint2);
CGPoint mid2 = midPoint(m_currentPoint, m_previousPoint1);
CGContextMoveToPoint(context1, mid1.x, mid1.y);
CGContextAddQuadCurveToPoint(context1, m_previousPoint1.x, m_previousPoint1.y, mid2.x, mid2.y);
CGContextSetLineCap(context1, kCGLineCapRound);
CGContextSetLineWidth(context1, self.lineWidth);
CGContextSetStrokeColorWithColor(context1, self.lineColor.CGColor);
CGContextSetFlatness(context1, 2.0);
CGContextSetAllowsAntialiasing(context1, true);
CGContextStrokePath(context1);
}
问候
Ranjit
RegardsRanjit
推荐答案
@hfossli发布的链接已经死了,但这里是已归档的内容:
The link posted by @hfossli is now dead, but here is the archived content:
CGLayer不再推荐
CGLayer no longer recommended
发布者:robnapier于2012年7月13日在图书更新
I花很多时间在WWDC的实验室里提问并与开发人员交谈。这次我坐下了核心图形工程师并向他们询问了我最喜欢的一个未充分利用的工具:CGLayer,我将在第6章结束时讨论.CGLayer听起来是一个好主意:专为在屏幕上绘图而优化的绘图环境,通过硬件优化。可能会出现什么问题?
I spend a lot of time in the labs at WWDC asking questions and talking with the developers. I sat down the Core Graphics engineers this time and asked them about one of my favorite underused tools: CGLayer, which I discuss at the end of Chapter 6. CGLayer sounds like a great idea: a drawing context optimized specifically for drawing on the screen, with hardware optimization. What could go wrong?
我开始怀疑CGLayer总是一场伟大的胜利。如果您的图层太大而无法存储在GPU纹理中,该怎么办? CGLayer被广告用作您反复绘制的标记。将数据移入GPU和从GPU移动数据非常昂贵。也许CGLayer没有意义,除非你画了一定次数。文档没有给出任何指导。
I started to have doubts, though, that CGLayer was always a great win. What if your layers were too large to store in GPU textures? CGLayer is advertised for use as a "stamp" that you repeatedly draw. Moving data to and from the GPU is expensive. Maybe CGLayer doesn’t make sense unless you draw it a certain number of times. The docs give no guidance on this.
所以我问核心图形团队我什么时候应该使用CGLayer?
So I asked the Core Graphics team "When should I be using CGLayer?"
从不。
...... ???决不?但是对于冲压吧?
… ??? Never? But for stamping right?
从不。
所以我们谈了一些。似乎CGLayer是纸上听起来很棒的东西之一,但并不总是在实践中发挥作用。有时它更快。有时它慢。当它变得更快时,没有简单的规则。随着时间的推移,似乎他们已经悄然放弃了它而没有实际弃用它。我已经要求更新文档以符合Apple目前的建议。自2006年以来,CGLayer Reference尚未更新。
So we talked some more. It appears that CGLayer was one of those things that sounded great on paper, but just doesn’t always work in practice. Sometimes it’s faster. Sometimes its slower. There’s no easy rule for when it’s going to be faster. Over time it seems they’ve quietly abandoned it without actually deprecating it. I’ve asked that the docs be updated to match Apple’s current recommendation. The CGLayer Reference hasn’t been updated since 2006.
我收到的建议是使用CGBitmapContext或CALayer进行标记。
The recommendation I received was to use CGBitmapContext or CALayer for stamping.
这篇关于如何使用CgLayer进行最佳绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!