使用 CoreGraphics 在 iPhone 上绘制径向数据
我正在尝试使用 CoreGraphics 在地图上绘制雷达数据。之前,我尝试通过围绕每个单独的数据点创建路径,然后填充适当的颜色来单独绘制每个数据点。因此,对于此方法,我必须有 4 个位置点和每个数据点的数据值。事实证明,这样做成本太高,因为每个图像都有 30,000 到 80,000 个数据点。
雷达数据以径向格式存储(360 个径向线,每个包含 230 个数据点)。因此,我只循环径向线,而不是循环每个数据点。我没有围绕每个数据点的路径,而是围绕每个径向的路径。然后,我使用该特定渐变的颜色数组创建并绘制线性渐变 (CGGradientCreateWithColors)。
这个方法画起来比以前快了很多,但是还是不够快。另一个问题是放大时数据的“平滑度”。径向上每个数据点之间的渐变混合,因此您看不到单个像素。我不希望它这样做。我认为这是我最大的性能损失来自(计算每个数据点之间的梯度)。我宁愿将每个数据点绘制为离散颜色。这看起来会更好,而且性能应该会更好。
有没有一种方法可以为路径提供一组颜色(类似于我对渐变所做的操作),从而离散地绘制它们。我需要能够绘制它,而不必返回绘制各个数据点。
先感谢您, 罗斯
编辑
第一张图片是我进行绘图的原始方式(通过单独绘制每个数据点)。这就是我希望它最终的样子。
第二张图是渐变,比之前效率高了(虽然效率不够),但是看起来不太对劲。
I am trying to draw radar data on top of a map using CoreGraphics. Before, I was trying to draw each data point individually by making a path around each individual data point and then filling the appropriate color. So for this method I had to have 4 location points and a data value for each data point. Doing it this way turned out to be way too costly, as there are between 30,000 and 80,000 data points for each image.
The radar data is stored in a radial format (360 radials, each containing 230 data points). So instead of looping through every data point, I only loop through the radials. And instead of a path around each data point, I have a path around each radial. I then create and draw a linear gradient (CGGradientCreateWithColors) using the array of colors for that particular gradient.
This method is drawing a lot faster than before, but it is still not fast enough. Another issue with it is the "smoothness" of the data when I zoom in. The gradient blends between every data point in the radial so you cannot see the individual pixels. I do not want it to do this. I think this is where my biggest performance hit is coming from (computing the gradient between each data point). I would rather each data point be drawn as a discrete color. This would look better and it should perform better.
Is there a way to provide an array of colors to a path (similar to what I did with the gradient) that will draw them discretely. I need to be able to draw this without having to go back to drawing the individual data points.
Thank you in advance,
Ross
EDIT
The first picture is the original way I was doing the drawing (by drawing each data point individually). This is how I want it to look in the end.
The second picture is the gradient, which is more efficient than before (not efficient enough though), but does not look right.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我很难想象你想要画什么(很难想象在 iPhone 屏幕上画 80,000 个东西,并且每个东西都比一个像素大……),但这里有一些你的想法。
首先,如果您有一致的东西需要重复绘制,请将其放在
CGLayer
上。不是CALayer
,而是CGLayer
。这是一个硬件优化的对象,用于重复“标记”同一事物。如果您需要更改颜色甚至应用渐变,您可以使用遮罩或合成 CGImage 来应用这些内容(如果您可以重用生成的图像而不必缩放,则后者通常会更快)任何事物)。尽可能预先计算梯度并将其存储在数据结构中。尝试在
drawRect:
中计算它们通常会对性能造成重大影响。同样,预先计算您的路径,这样您就不必在每次绘制时都构建它们。如果您可以避免缩放图层,则可以将其
shouldRasterize
设置为YES
,这通常会在图层复杂时提高性能。另一方面,如果您需要进行缩放或其他变换,那么在 CALayer 上绘图并应用所需的变换可能会非常快。您还可以将转换应用于整个UIView
,但如果您需要转换大量单独的内容,那么这就是CALayer
的全部内容。 (对于前面的一点,您当然可以将CGLayer
绘制到CALayer
上。)我不明白您所说的“离散”绘制路径是什么意思。路径是由控制点定义的连续线。
编辑:根据您的编辑,我建议尝试使用索引色彩空间 (
CGColorSpaceCreateIndexed()
)。这样,您的渐变不应包含中间颜色,并且整个绘图的计算速度应该更快。您还应该查看
CGShading
。 (我相信它适用于 iOS,而不仅仅是 Mac。)它可能比CGGradient
更接近您想要的。I'm having trouble picturing quite what you're trying to draw (it's hard to imagine drawing 80,000 of anything on an iPhone screen and have each thing be bigger than a pixel....) but here are some thoughts along your way.
First, if you have a consistent thing to draw repeatedly, put it on a
CGLayer
. Not aCALayer
, aCGLayer
. This is a hardware-optimized object for "stamping" the same thing repeatedly. If you need to change colors or even apply a gradient, you can apply these using a mask or by compositing aCGImage
(the latter is generally faster if you can then reuse the resulting image and not have to scale anything).As much as possible, pre-compute your gradients and store them in data structures. Trying to compute them in
drawRect:
is often a major performance hit. Similarly, pre-compute your paths so that you don't have to build them every time you draw.If you can avoid scaling your layers, you can set their
shouldRasterize
toYES
, which will generally improve performance when you have complex layers. On the other hand, if you need to do scaling or other transforms, then drawing on aCALayer
and applying the needed transform can be very fast. You can also apply transforms to the wholeUIView
, but if you need lots of separate things transformed, then that's whatCALayer
is all about. (To the earlier point, you can of course draw aCGLayer
onto aCALayer
.)I don't understand what you mean by drawing a path "discretely." A path is a continuous line defined by control points.
EDIT: Based on your edits, what I recommend experimenting with is an indexed colorspace (
CGColorSpaceCreateIndexed()
). This way your gradient shouldn't be able to include intermediate colors, and calculations should be faster on the entire drawing.You should also look at
CGShading
. (I believe it's available for iOS and not just Mac.) It may be closer to what you want thanCGGradient
.