在窗户上制作蓝色小方块时出现问题

发布于 2025-01-06 19:19:27 字数 789 浏览 0 评论 0原文

这是我第一次涉足 Objective-c 图像世界,我立即遇到了困难。我拥有的是 TimeAdder.xib 中的自定义 NSWindow,它由名为 TimeAdder 的 NSWindowController 控制。一切都工作得很好,现在我尝试添加我的小蓝色矩形。

我将一个大小为 16 x 16 的 NSImageView 设置到窗口上所需的位置。然后我将以下代码添加到 windowDidLoad 方法中。

NSSize size;
size.width = 16;
size.height = 16;
image1 = [[NSImage alloc] initWithSize:size];
NSRect   imageBounds = NSMakeRect (0, 0, size.width, size.height);
NSColor* fileSet1Color = [NSColor blueColor];

[image1 lockFocus];
[fileSet1Color set];
NSRectFill (imageBounds);
[image1 unlockFocus];
[pickImage1 setImage:image1];

pickImage1 是 NSImageView 的名称,并且是 TimeAdder 中的 ivar。 image1 是 TimeAdder 中的 ivar,并且是 NSImage。 size、firstSet1Color、imageBounds 都是局部变量,在方法完成后就会消失。不确定这是否重要。

该代码执行并不会导致任何类型的异常,但不会执行任何操作。有人对可能发生的事情有任何想法吗?

This is my first foray into the world of Objective-c images and I immediately ran into difficulty. What I have is a custom NSWindow in TimeAdder.xib which is controlled by an NSWindowController named TimeAdder. Everything is working very well and now I try to add my little blue rectangle.

I set up an NSImageView with a size of 16 by 16 into the desired spot on the window. Then I added the following code into the windowDidLoad method.

NSSize size;
size.width = 16;
size.height = 16;
image1 = [[NSImage alloc] initWithSize:size];
NSRect   imageBounds = NSMakeRect (0, 0, size.width, size.height);
NSColor* fileSet1Color = [NSColor blueColor];

[image1 lockFocus];
[fileSet1Color set];
NSRectFill (imageBounds);
[image1 unlockFocus];
[pickImage1 setImage:image1];

pickImage1 is the name of the NSImageView and is an ivar within TimeAdder. image1 is an ivar within TimeAdder and is an NSImage. size, firstSet1Color, imageBounds are all local variables and go away when the method is done. Wasn't sure if that mattered or not.

The code executes and doesn't cause any kind of exception but doesn't do anything. Anybody out there have any thoughts as to what might be going on?

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

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

发布评论

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

评论(1

浮世清欢 2025-01-13 19:19:28
  1. 你确定pickImage1实际上是一个NSImageView吗?如果您没有将其连接到笔尖,或者连接不正确,则可能为零。在这种情况下,Objective-C 不会引发异常,而是默默地不执行任何操作。

  2. 不太可能:通过将 NSImage 转储到文件中,确保 NSImage 实际上包含您认为它所做的内容。最简单的方法是 [[image1 TIFFRepresentation] writeToFile:@"/tmp/image.tiff"atomically:YES],然后在终端窗口中,打开 /tmp/image.tiff.

(如果可以的话,请不要在实际代码中使用 -TIFFRepresentation ,但它对于像这样的调试和健全性检查来说很好。)

  1. Are you sure pickImage1 is in fact an NSImageView? If you didn't hook it up in the nib, or did it incorrectly, it might be nil. Objective-C doesn't raise an exception in that case, but instead silently does nothing.

  2. Less likely: Make sure that NSImage actually contains what you think it does, by dumping it to a file. Easiest way is [[image1 TIFFRepresentation] writeToFile:@"/tmp/image.tiff" atomically:YES], then in a Terminal window, open /tmp/image.tiff.

(Please don't use -TIFFRepresentation in real code if you can help it, but it's fine for debugging and sanity checks like this.)

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