Todd
Todd

Reputation: 1953

NSImage is 'retained' between drag operations

Hi I have implemented:

- (NSImage *)dragImageForRowsWithIndexes:(NSIndexSet *)dragRows tableColumns:(NSArray *)tableColumns event:(NSEvent *)dragEvent offset:(NSPointPointer)dragImageOffset
{
    NSImage *dragImage = [NSImage imageNamed:@"Drag-Icon.png"];
    NSInteger numberOfItems = dragRows.count;
    NSAttributedString *numbers = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%lu",numberOfItems] attributes:attributes];
    [dragImage lockFocus];
    NSRect numbersSurroundRect = NSMakeRect(dragImage.size.width - (numbers.size.width + 15) - strokeWidth - 5, strokeWidth, numbers.size.width + 15, boxHeight);
    NSBezierPath *circle = [NSBezierPath bezierPathWithRoundedRect:numbersSurroundRect xRadius:9.0 yRadius:9.0];
    [[NSColor redColor] set];
    [circle fill];
    [[NSColor whiteColor] set];
    [circle setLineWidth:strokeWidth];
    [circle stroke];
    numbersSurroundRect.origin.y += ((numbersSurroundRect.size.height - numbers.size.height) + 1.75);
    [numbers drawInRect:numbersSurroundRect];
    [dragImage unlockFocus];
    return dragImage;
}

In my NSTableView sub-class and when I drag a number of rows in my table I get:

enter image description here

Then when I drag only 1 or two rows, I get:

enter image description here

It appears that the old row count is returned and then the new count drawn on top of it....

Could anyone shed any light on this, please? Is it a result of using ARC in my project and somehow the

Upvotes: 2

Views: 119

Answers (1)

Ken Aspeslagh
Ken Aspeslagh

Reputation: 11594

You are drawing directly onto the original image. You should make a copy before you draw on it so you can draw on a fresh copy the next time.

Upvotes: 1

Related Questions