- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我创建了一个简单的绘图项目,代码工作正常,但我想将绘图缓存到 CGlayer 中,因为我读到它更有效的绘图方式。我已通读文件,但无法正确理解。所以 friend 们,我请求你们在这方面帮助我。
下面是我的代码,我想知道如何在这里面使用CgLayer
- (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);
}
问候兰 git
最佳答案
@hfossli 发布的链接现已失效,但这是存档内容:
不再推荐CGLayer
robnapier 于 2012 年 7 月 13 日在图书更新中发布
我在 WWDC 的实验室里花了很多时间提问并与开发人员交谈。这次我让 Core Graphics 工程师坐下来,向他们询问了我最喜欢的未被充分利用的工具之一:CGLayer,我在第 6 章末尾讨论了它。CGLayer 听起来是个好主意:一个专门为在屏幕上绘图而优化的绘图上下文,通过硬件优化。会出什么问题?
不过,我开始怀疑 CGLayer 是否总是一个伟大的胜利。如果您的层太大而无法存储在 GPU 纹理中怎么办? CGLayer 被宣传为用作您反复绘制的“邮票”。将数据移入和移出 GPU 的成本很高。除非你绘制一定次数,否则 CGLayer 可能没有意义。文档没有对此提供任何指导。
所以我问 Core Graphics 团队“我什么时候应该使用 CGLayer?”
“从不。”
……???绝不?但是为了冲压对吗?
从不。
所以我们又谈了一些。看起来 CGLayer 是那些在纸面上听起来很棒但在实践中并不总是有效的东西之一。有时它更快。有时它更慢。对于何时会更快,没有简单的规则。随着时间的推移,他们似乎已经悄悄地放弃了它而没有真正弃用它。我已经要求更新文档以符合 Apple 当前的建议。 CGLayer 引用自 2006 年以来就没有更新过。
我收到的建议是使用 CGBitmapContext 或 CALayer 进行标记。 对于第 131-132 页给出的具体示例,CATextLayer 可能是最好的工具。请记住,您可以使用 initWithLayer: 轻松克隆 CALayer。 (John Mueller 在下面指出这实际上不受支持。)
关于ios - 如何使用 CgLayer 进行最佳绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11341763/
我正在使用 CGlayers 进行绘图,如文档所述,这是在 Canvas 上渲染绘图的更有效方式。 我基本上是绘制到 CGlayers 中,然后使用 CGContextDrawLayerInRect
我正在使用 CGlayers 进行绘图。我已经实现了绘图部分,用户将在其中绘制的 drawingView(canvas) 是动态的,我的意思是,用户可以增加/减少 drawingView(Canvas
我正在为我的 iOS5 应用程序创建一些 CGLayer,如下所示: layer_1 = CGLayerCreateWithContext (context,self.bounds.size, NUL
我想从同一个 for 循环中将对象绘制到两个单独的 CGLayer,但不确定如何执行此操作。 例如,我想在三个蓝色圆圈后面画三个橙色圆圈,橙色圆圈在一层,蓝色圆圈在另一层。以下代码会将每个圆圈放在前一
最终我正在开发一个在 iPhone 上使用的方框模糊功能。 该函数将获取 UIImage 并首先在侧面绘制透明副本,然后获取该图像并在上方和下方绘制透明副本,返回一个非常模糊的图像。 阅读Drawin
我正在画几个CGPaths在iPad 上的drawRect 方法中的Cocoa View 中。我开始将它们直接绘制到 UIGraphicsGetCurrentContext()上下文,但是当我的路径变
我正在使用绘图应用程序,我使用 CGlayers 进行绘图。触摸结束时,我从图层中获取图像并将其存储在数组中,我用它来撤消操作。 我的触摸结束功能 -(void)touchesEnded:(NSSet
我在 CgLayer 上使用 unod 重做操作,我已经尝试了一些代码,但无法让它工作,不知道,我哪里错了,下面是我写的代码 这是我的 drawRect 函数 - (void)drawRect:(CG
我创建了一个简单的绘图项目,代码工作正常,但我想将绘图缓存到 CGlayer 中,因为我读到它更有效的绘图方式。我已通读文件,但无法正确理解。所以 friend 们,我请求你们在这方面帮助我。 下面是
我一直在四处寻找答案。我仍在加快 iOS 开发的速度,所以我绝对可以接受我在这里可能很笨。然而,完全没有答案让我怀疑是否真的有答案。 事情是这样的:我正在使用 CGLayer 生成一个相当基本的矢量多
我正在使用一个绘图应用程序,我在 CGlayers 中绘图,然后在图形上下文中绘图,但我的绘图变得模糊。 这是我的代码 - (void)drawRect:(CGRect)rect { CGConte
我想在 CGLayer 上绘制一个简单的 UIImage,但找不到任何有用的资源来完成它。请告诉我如何在 CGLayer 上绘制 UIImage。 谢谢! 最佳答案 您要找的是这个吗? CGConte
意图:在屏幕外创建CGLayer,从第二个线程在其上绘制一些复杂的东西,并稍后在主线程上使用它快速绘制 问题: CGLayerCreateWithContext(context, size, info
我有 UIView 子类 - 实际上是一个拼图 - 在 initWithFrame 处初始化了 2 个不同的 CGLayer。 在drawRect中,我必须混合这两层,并且最上面应该有可变alpha,
当你像这样创建一个CGLayer,然后获取上下文时...似乎不可能释放CGContextRef? 释放 CGLayerRef 本身(显然)效果很好。 您认为可以在释放 CGLayer 之前释放 CGC
我们不断地制作一个CGLayer,进行处理,然后发布它。这种情况实时发生很多次。当然,每次制作一个全新的 CGLayer 都会有很多开销。所以... 当然,最好保留该图层,并每次删除其中的所有数据——
我有自定义的 UICollectionViewCell 子类,我在其中使用剪裁、描边和透明度进行绘制。它在模拟器和 iPhone 5 上运行良好,但在旧设备上存在明显的性能问题。 所以我想把耗时的绘图
我编写了一个 iOS 应用程序,我在其中非常成功地使用了 CGLayer。在研究从该应用程序中获得更多性能的方法时,我看到了这篇博文:http://iosptl.com/posts/cglayer-n
我正在尝试提高我们应用程序中的滚动性能。我遵循了所有普遍接受的建议(用 CG 自己绘制、单元格不透明、没有 subview 等),但当我们有后台 CPU 和网络事件时,有时它仍然会卡顿。 这里列出了一
请注意,这个问题是关于CGLayer(您通常用它来在屏幕外绘制),而不是关于CALayer。 在 iOS 中,将 CGLayer 保存为 PNG 文件的正确代码是什么?谢谢! 再说一遍,那是 CGLa
我是一名优秀的程序员,十分优秀!