views:

122

answers:

2

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?

+3  A: 

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?

NSResponder
+4  A: 

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.

Ken