NSImage transparency

I am trying to set a custom drag and drop icon for use in an NSTableView. Everything seems to work, but I ran into a problem due to my inexperience with Quartz.

- (NSImage *)dragImageForRowsWithIndexes:(NSIndexSet *)dragRows tableColumns:(NSArray *)tableColumns event:(NSEvent *)dragEvent offset:(NSPointPointer)dragImageOffset
{
 NSImage *dragImage = [NSImage imageNamed:@"icon.png"];
 NSString *count = [NSString stringWithFormat:@"%d", [dragRows count]];

 [dragImage lockFocus]; 
 [dragImage compositeToPoint:NSZeroPoint fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:0.5];
 [count drawAtPoint:NSZeroPoint withAttributes:nil];

 [dragImage unlockFocus];
 return dragImage;
}

      

Basically I want to make my icon.png file with 50% opacity along with an NSString that shows the number of lines being dragged. The problem I see is that my NSString is rendering at low opacity, but not my icon.

+1


source to share


1 answer


The problem is you are drawing your icon on top of yourself. What you probably want is something like this:



- (NSImage *)dragImageForRowsWithIndexes:(NSIndexSet *)dragRows tableColumns:(NSArray *)tableColumns event:(NSEvent *)dragEvent offset:(NSPointPointer)dragImageOffset
{
 NSImage *icon = [NSImage imageNamed:@"icon.png"];
 NSString *count = [NSString stringWithFormat:@"%lu", [dragRows count]];

 NSImage *dragImage = [[[NSImage alloc] initWithSize:[icon size]] autorelease];

 [dragImage lockFocus]; 
 [icon drawAtPoint:NSZeroPoint fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:0.5];
 [count drawAtPoint:NSZeroPoint withAttributes:nil];

 [dragImage unlockFocus];
 return dragImage;
}

      

+3


source







All Articles