简单的 NSImage 绘图无法按预期工作

发布于 2024-08-17 10:28:25 字数 485 浏览 7 评论 0原文

这没有任何作用:

NSImage* testImage = [[NSImage alloc] initWithSize:NSMakeSize(2.0,2.0)];

[testImage lockFocus];
[[NSImage imageNamed:@"testImage"] drawAtPoint:NSMakePoint(1.0,1.0) fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
[testImage unlockFocus];

[levelView setImage:testImage];

...但是这确实:

[levelView setImage:[NSImage imageNamed:@"testImage"]];

在我看来,如果后者产生可见的结果,那么前者也应该产生可见的结果。我想我在某个地方犯了一个愚蠢的错误?

This doesn't do anything:

NSImage* testImage = [[NSImage alloc] initWithSize:NSMakeSize(2.0,2.0)];

[testImage lockFocus];
[[NSImage imageNamed:@"testImage"] drawAtPoint:NSMakePoint(1.0,1.0) fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
[testImage unlockFocus];

[levelView setImage:testImage];

...but this does:

[levelView setImage:[NSImage imageNamed:@"testImage"]];

Seems to me if the latter produces visible results, so should the former. I assume I'm making a stupid mistake somewhere?

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

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

发布评论

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

评论(2

番薯 2024-08-24 10:28:25

让我们调用 [NSImage imageNamed:@"testImage"] sourceImage。

testImage 将为 2x2 像素。由于您在 (1,1)、原点上方 1 个像素和右侧 1 个像素处绘制 sourceImage,因此四个像素中的 3 个必然仍然是清晰的。右上角的像素将与 sourceImage 的左下角的像素相同。如果这很清楚,你就不会看到任何东西。

Let's call [NSImage imageNamed:@"testImage"] sourceImage.

testImage will be 2x2 pixels. Since you're drawing sourceImage at (1,1), 1 pixel up and one pixel right of the origin, 3 of your four pixels are necessarily still going to be clear. The top right pixel will be the same as the bottom left pixel of sourceImage. If that's clear, you aren't going to see anything.

能否归途做我良人 2024-08-24 10:28:25

我在这里没有看到足够的代码来诊断问题。您如何调用第一个清单中的代码? “levelView”是否已在窗口中,或者此时是否已从笔尖加载?

您的项目中是否有名为“testImage”的图像文件?您确定希望该图像只有两个像素的正方形吗?

I'm not seeing enough code here to diagnose the problem. How are you invoking the code in your first listing? Is "levelView" already in a window, or has it been loaded from a nib at that point?

Is there an image file named "testImage" in your project? Are you sure that you want this image to only be two pixels square?

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