简单的 NSImage 绘图无法按预期工作
这没有任何作用:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
让我们调用 [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.
我在这里没有看到足够的代码来诊断问题。您如何调用第一个清单中的代码? “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?