CALayer vs CGContext,这是一种更好的设计方法吗? [英] CALayer vs CGContext, which is a better design approach?

查看:91
本文介绍了CALayer vs CGContext,这是一种更好的设计方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试iOS绘图。为了做一个实际练习,我写了一个BarChart组件。以下是类图(嗯,我不允许上传图像)所以让我用文字写。我有一个继承自UIView的NGBarChartView有2个协议NGBarChartViewDataSource和NGBarChartViewDelegate。代码位于 https://github.com/mraghuram/NELGPieChart/blob/master/NELGPieChart /NGBarChartView.m

I have been doing some experimenting with iOS drawing. To do a practical exercise I wrote a BarChart component. The following is the class diagram (well, I wasnt allowed to upload images) so let me write it in words. I have a NGBarChartView which inherits from UIView has 2 protocols NGBarChartViewDataSource and NGBarChartViewDelegate. And the code is at https://github.com/mraghuram/NELGPieChart/blob/master/NELGPieChart/NGBarChartView.m

为了绘制barChart,我创建了每个barChart作为不同的CAShapeLayer。我这样做的原因有两个,首先我可以创建一个UIBezierPath并将其附加到CAShapeLayer对象和两个,我可以通过使用[Layer hitTest]方法轻松跟踪是否触摸了barItem。该组件工作得很好。但是,我对绘制barCharts的方法感到不舒服。因此,这个说明。我需要以下专家意见

To draw the barChart, I have created each barChart as a different CAShapeLayer. The reason I did this is two fold, first I could just create a UIBezierPath and attach that to a CAShapeLayer object and two, I can easily track if a barItem is touched or not by using [Layer hitTest] method. The component works pretty well. However, I am not comfortable with the approach I have taken to draw the barCharts. Hence this note. I need expert opinion on the following


  1. 通过使用CAShapeLayer并创建BarItems我真的不是
    使用UIGraphicsContext,这是一个好的设计?

  2. 我的方法将在UIView中创建几个CALayer。根据性能,是否有
    限制,你可以在UIView中创建
    的CALayers数量。

  3. 如果一个好的选择是使用CGContext *方法然后,确定特定路径是否被触摸的
    正确方式

  4. 从动画的角度来看,例如当你
    点击时,条形图闪烁它,更好的层设计或CGContext设计
    更好。

  1. By using the CAShapeLayer and creating BarItems I am really not using the UIGraphicsContext, is this a good design?
  2. My approach will create several CALayers inside a UIView. Is there a limit, based on performance, to the number of CALayers you can create in a UIView.
  3. If a good alternative is to use CGContext* methods then, whats the right way to identify if a particular path has been touched
  4. From an Animation point of view, such as the Bar blinking when you tap on it, is the Layer design better or the CGContext design better.

非常感谢帮助。顺便说一句,你可以自由地查看我的代码和评论。我很乐意接受任何改进建议。

Help is very much appreciated. BTW, you are free to look at my code and comment. I will gladly accept any suggestions to improve.

最好,
Murali

Best, Murali

推荐答案

IMO,一般来说,任何一种绘图形状都需要繁重的处理能力。使用GPU合成缓存位图比再次绘制所有这些位图非常便宜。因此,在许多情况下,我们将所有绘图缓存到位图中,而在iOS中, CALayer 负责该操作。

IMO, generally, any kind of drawing shapes needs heavy processing power. And compositing cached bitmap with GPU is very cheap than drawing all of them again. So in many cases, we caches all drawings into a bitmap, and in iOS, CALayer is in charge of that.

无论如何,如果您的位图超出视频内存限制, Quartz 不能立即合成所有图层。因此,Quartz必须在多个阶段绘制单帧。这需要将一些纹理重新加载到GPU中。这可能会影响性能。我不确定这一点,因为已知iPhone VRAM与系统RAM集成。无论如何,即使是这种情况,它仍然需要更多的工作。如果即使系统内存不足,系统也可以清除现有的位图,并要求稍后重新绘制它们。

Anyway, if your bitmaps exceed video memory limit, Quartz cannot composite all layers at once. Consequently, Quartz have to draw single frame over multiple phases. And this needs reloading some textures into GPU. This can impact on performance. I am not sure on this because iPhone VRAM is known to be integrated with system RAM. Anyway it's still true that it needs more work on even that case. If even system memory becomes insufficient, system can purge existing bitmap and ask to redraw them later.


  1. CAShapeLayer 将完成所有 CGContext (我相信你的意思是这个)而不是你。如果你觉得需要更低级别的优化,你可以自己做。

  1. CAShapeLayer will do all of CGContext(I believe you meant this) works instead of you. You can do that yourself if you felt needs of more lower level optimization.

是的。显然,一切都受到性能观点的限制。如果您使用具有大型alpha混合图形的数百个图层,则会导致性能问题。无论如何,通常情况下,它不会发生,因为GPU加速了图层组合。如果您的图形线不是那么多,并且它们基本上是不透明的,那么你会没事的。

Yes. Obviously, everything has limit by performance view. If you're using hundreds of layers with large alpha-blended graphics, it'll cause performance problem. Anyway, generally, it doesn't happen because layer composition is accelerated by GPU. If your graph lines are not so many, and they're basically opaque, you'll be fine.

所有你必须知道的是一旦图形绘图是合成,没有办法将它们分解回来。因为组合本身是有损压缩的一种优化,所以你只有两个选项(1)在需要变异时重绘所有图形。或者(2)制作每个显示元素的缓存位图(如图线),并根据需要进行复合。这就是CALayers正在做的事情。

All you have to know is once graphics drawings are composited, there is no way to decompose them back. Because composition itself is a sort of optimization by lossy compression, So you have only two options (1) redraw all graphics when mutation is required. Or (2) Make cached bitmap of each display element (like graph line) and just composite as your needs. This is just what the CALayers are doing.

绝对基于图层的方法要好得多。任何类型的自由形状绘图(即使它在GPU内完成)也需要比GPU更简单的位图合成(将成为两个纹理三角形)更多的处理能力。当然,除非您的图层不超过视频内存限制。

Absolutely layer-based approach is far better. Any kind of free shape drawing (even it's done within GPU) needs a lot more processing power than simple bitmap composition (which will become two textured triangles) by GPU. Of course, unless your layers doesn't exceeds video memory limit.

我希望这会有所帮助。

这篇关于CALayer vs CGContext,这是一种更好的设计方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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