cocoansrect

cocoa - draw image in NSRect


I'm having difficulties drawing an image in an NSRect, the code below creates an NSRect at the desired position and also puts an image in. The problem is that the image is a circle with a transparent areas and these get filled in black, so instead of having a circle I have a black square with the circle img in it...

- (void)drawRect:(NSRect)dirtyRect{

NSImage *bg = [NSImage imageNamed:@"citydot_c1.png"];
NSRect city = NSMakeRect(10, 10, 8, 8);
[bg drawInRect:city fromRect:NSZeroRect operation:NSCompositeSourceAtop fraction:1.0f];

NSFrameRect(city);
}

So basically, I need a way to remove the background color of a NSRect, I think...

Anyone got any ideas?


Solution

  • You probably want to compose the image using Porter-Duff "Over":

    [bg drawInRect:city fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0f];
    

    Using this blend operation you get normal transparency composition.