0

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

Lennart
  • 1,560
  • 5
  • 20
  • 38
  • 2
    Can you explain that last line a bit more? What is the `map` variable in this context, and why does it need to draw the same rect that you make for your image? – Tim Oct 22 '12 at 17:14
  • sorry, I forgot to say that, map is the NSView... – Lennart Oct 22 '12 at 21:23

1 Answers1

2

You never call drawRect: directly. This routine has various pre-conditions that are provided by Cocoa, such as the creation of a CGContextRef. You implement drawRect:. Cocoa calls it.

Your drawInRect:fromRect:operation:fraction: call should be put into the drawRect: of map, which should be a subclass of NSView. This specific problem is usually better solved with an NSImageView rather than a custom NSView, but if the drawing is more complex, then a custom NSView is appropriate.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • What would you recommend for the following situation: what I would like to achieve is a map, and I have 6 images which together create a map of the world this should then be somehow draggable and zoomable, so imagine it a bit like google maps... NSView or NSImageView? – Lennart Oct 23 '12 at 13:13
  • To make something much larger than the screen drag and zoom with good performance, you often want CATiledLayer. But if this is your first app, you may want to start with something simpler, and just put 6 imageviews in a scrollview. For something only 6x the size of the screen, it may be fast enough. – Rob Napier Oct 23 '12 at 13:47
  • How did you guess this was my first app? :P anyway the scroll view idea is good it sound simpler, I'll then try to go to a CATiledLayer. Thanks for your help :) – Lennart Oct 23 '12 at 13:50