如何确保 Mac 上的绘制操作像素对齐?

发布于 2024-10-17 18:07:14 字数 144 浏览 8 评论 0原文

在最近的 iOS techtalk 中,我听到了一个关于“确保你的绘制操作是像素对齐的”的建议。

这是对 Mac/iOS 绘图性能的有效建议吗?

另一个问题是我如何确定我的代码正在以像素对齐方式绘制?
有什么工具或技巧可以提供帮助吗?

In recent iOS techtalk, I heard a suggestion about "make sure your draw operations are pixel aligned".

Is this a valid suggestion for Mac/iOS drawing performance?

Another question is how I can determine my code is drawing with pixel aligned?
Is there any tools or tricks can help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我家小可爱 2024-10-24 18:07:14

像素对齐与性能无关,但与渲染图形的质量有关。

解释它的最好方法是显示一些代码:

//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];

如果您尝试此代码,您将看到一条红线。如果仔细观察,这条线不会很锐利 - 红色会被冲掉,宽度看起来大约有 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];

执行此代码,您将看到您最初可能想要的输出。

关于这一点有很多可以说的,但基本上将你的分数抵消 0.5,你应该没问题。

Pixel alignment doesn't have anything to do with performance, but with the quality of the rendered graphics.

The best way to explain it is to show a little bit of code:

//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];

If you try out this code you will see a red line. If you look closely the line will not be very sharp - the red will be washed out and the width will look like it is about 2 pixels wide. The reason for this is that drawing in Cocoa uses points, not pixels. The result is that the line is being drawn between two pixels. (For more information please read through the cocoa drawing guide in the docs.)

Now, if we make a couple of simple changes...

//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];

Execute this code and you will see the output you probably originally intended.

There is a lot that can be said about this but essentially offset your points by 0.5 and you should be OK.

心凉怎暖 2024-10-24 18:07:14

对于 OS X 10.7+,您还需要使用 backingAlignedRect:options: 生成一个“..在窗口中支持存储像素对齐的矩形坐标。”。

With OS X 10.7+ you'd want to also use backingAlignedRect:options: to produce a "..backing store pixel aligned rectangle in window coordinates.".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文