Question

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?

Was it helpful?

Solution

  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.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top