I’m having difficulties understanding why the below code doesn’t work, what I want to achieve is an image being displayed in the top left corner of a NSView, but nothing is showing…
NSImage *map0 = [NSImage imageNamed:@"map0.png"];
NSRect rect = NSMakeRect(0, 0, 400, 400);
[map0 drawInRect:rect fromRect:NSZeroRect operation:NSCompositeSourceAtop fraction:1.0f];
[map drawRect:rect];
EDIT:
map is the NSView into which I would like to draw the image into
You never call
drawRect:directly. This routine has various pre-conditions that are provided by Cocoa, such as the creation of aCGContextRef. You implementdrawRect:. Cocoa calls it.Your
drawInRect:fromRect:operation:fraction:call should be put into thedrawRect:ofmap, which should be a subclass ofNSView. This specific problem is usually better solved with anNSImageViewrather than a customNSView, but if the drawing is more complex, then a customNSViewis appropriate.