提高手指画性能 [英] Improving Finger Painting Performance

查看:106
本文介绍了提高手指画性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单绘画:

指法iPhone屏幕为UIView绘制临时图形。触摸结束后,这些临时图形将被删除,然后存储到底层UIView中。

Simple Painting:
Fingering the iPhone screen paints temporary graphics to a UIView. These temporary graphics are erased after a touch ends, and are then stored into an underlying UIView.

这个过程很简单:

1) Touch Starts&移动>>

2)在顶级UIView上绘制临时图形>>

3)触摸结束>>

4)将临时图形传递给基础UIView >>

5)底层UIView将临时图形添加到存储图形>>

6)底层UIView重新绘制所有存储图形>>

7)删除顶部UIView上的临时图形。

The process is simple:
1) Touch Starts & Moves >>
2) Paint Temporary Graphics on top UIView>>
3) Touch Ends >>
4) Pass Temporary Graphics To underlying UIView >>
5) Underlying UIView adds Temporary Graphics to Stored Graphics >>
6) Underlying UIView Re-Draws all Stored Graphics >>
7) Delete Temporary Graphics on top UIView.

通过这种方式,我可以在底层UIView上累积图形,同时保持顶部UIView上的临时图形的响应性绘制。

In this manner, I can accumulate graphics on the underlying UIView while maintaining responsive painting of the temporary graphics on the top UIView.

(旁注:每个绘图只是一个自定义点对象的NSArray,它只是CGPoints的NSObject容器。而底层的UIView有一个单独的NSArray,它存储了这些CGPoints的NSArrays) / em>

(Sidenote: Each "Drawing" is simply an NSArray of custom "Point" Objects which are just NSObject containers for CGPoints. And the underlying UIView has a seperate NSArray, where it stores these NSArrays of CGPoints)

问题是:

当底层UIView上积累了大量图形时,它采取是时候把它全部画在屏幕上了。在完成底层存储图形的绘制之前,顶部UIView上的任何新绘图都不会显示。因此,当屏幕上出现许多图形时,会出现明显的延迟。

The Problem Is:
When a great deal of graphics has accumulated on the underlying UIView, it takes time to draw it all out on the screen. And any new drawings on the top UIView will not be displayed until the drawing of the underlying stored graphics is complete. Thus, there is a noticeable lag when many graphics are on the screen.

问题:

有谁能想到这是一个提高性能的好方法,当屏幕上有很多图形时,图纸之间没有明显的延迟?

Question:
Can anyone think of a good way to improve performance here, so that there is no noticable lag between drawings when there are a lot of graphics on the screen?

推荐答案

CGPoints的NSArray?你的意思是NSValues持有CGPoints的NSArray?这是一种非常耗费时间的方法,可以保存您不断访问的大量值。您可以以更好的方式存储此信息。代表整个屏幕的二维C阵列是最明显的。您可能还想查看位图图像表示,并直接绘制到CGImage而不是维护一堆CGPoints。看看在石英2D编程指南

An NSArray of CGPoints? You mean an NSArray of NSValues holding CGPoints? That's an incredibly time-expensive way to hold what has to be a huge number of values that you access constantly. You could store this information in many better ways. A 2-dimensional C-array representing the entire screen is the most obvious. You may also want to look into bitmap image representations, and draw directly into a CGImage rather than maintaining a bunch of CGPoints. Take a look at the Quartz 2D Programming Guide.

编辑:

您的对象(下方)相当于 NSValue ,只是更专业一点。当你有很多很多物体时,这里有很多开销(当我的屏幕几乎已满时,我估计约为100,000;如果你没有删除重复数据,则需要更多;运行仪器来对其进行分析)。旧式C数据结构可能要快得多,因为您可以避免所有保留/释放,分配等。但是还有其他选择。对于 NSMutableSet ,如果您对CGPoints进行像素对齐,并在您的CGPoints上重载 -isEqual ,则重复点检查会快得多点对象。

Your object (below) is the equivalent of an NSValue, just a little more specialized. There's a lot of overhead going on here when you have many, many objects (~100,000 I'm guessing when the screen is nearly full; more if you're not removing duplicates; run Instruments to profile it). Old-style C data structures are likely to be much faster for this, because you can avoid all the retains/releases, allocations, etc. There are other options, though. Duplicate point checking would be much faster with an NSMutableSet if you pixel-align your CGPoints, and overload -isEqual on your Point object.

确保您对像素进行像素对齐。在分数像素上绘制(并将它们全部存储起来),可以大大增加所涉及的对象数量以及您正在进行的绘制量。即使你想要抗锯齿,至少将像素舍入为.5(或.25或某些)。 CGPoint由两个双打组成。你不需要那种精确度来绘制到屏幕上。

Do make sure you're pixel-aligning your data. Drawing on fractional pixels (and storing them all), could dramatically increase the number of objects involved and the amount of drawing you're doing. Even if you want the anti-aliasing, at least round the pixels to .5 (or .25 or something). A CGPoint is made up of two doubles. You don't need that kind of precision to draw to the screen.

这篇关于提高手指画性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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