Jon*_*nny 2 performance cocoa objective-c ownerdrawn ios
在最近的iOS techtalk中,我听到了一个关于"确保你的绘制操作是像素对齐"的建议.
这是Mac/iOS绘图性能的有效建议吗?
另一个问题是我如何确定我的代码是用像素对齐绘制的?
有什么工具或技巧可以帮助吗?
像素对齐与性能无关,而是与渲染图形的质量有关.
解释它的最好方法是显示一些代码:
//Assume this is drawing in a rect such as a button or other NSView
NSBezierPath *line = [NSBezierPath bezierPath];
[line moveToPoint:NSMakePoint(0,0)];
[line lineToPoint:NSMakePoint(200,0)];
[[NSColor redColor] set];
[line stroke];
Run Code Online (Sandbox Code Playgroud)
如果您尝试此代码,您将看到一条红线.如果仔细观察,线条将不会非常清晰 - 红色将被冲洗掉,宽度看起来大约为2像素宽.原因是Cocoa中的绘图使用的是点,而不是像素.结果是在两个像素之间绘制线条.(有关更多信息,请阅读文档中的可可绘图指南.)
现在,如果我们做一些简单的改变......
//Assume this is drawing in a rect such as a button or other NSView
NSBezierPath *line = [NSBezierPath bezierPath];
[line moveToPoint:NSMakePoint(0,0.5)];
[line lineToPoint:NSMakePoint(200,0.5)];
[[NSColor redColor] set];
[line stroke];
Run Code Online (Sandbox Code Playgroud)
执行此代码,您将看到最初可能的输出.
关于这一点可以说很多,但基本上将你的积分抵消0.5,你应该没问题.