【问题标题】:NSImage is 'retained' between drag operationsNSImage 在拖动操作之间“保留”
【发布时间】:2013-01-11 12:27:42
【问题描述】:

您好,我已经实现了:

- (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;
}

在我的 NSTableView 子类中,当我在表格中拖动多行时,我得到:

然后当我只拖动一两行时,我得到:

似乎返回了旧的行数,然后在其上绘制了新的行数....

请问有人能解释一下吗?这是在我的项目中使用 ARC 的结果吗?

【问题讨论】:

    标签: macos cocoa drag-and-drop nstableview


    【解决方案1】:

    您正在直接在原始图像上绘图。您应该在绘制之前制作一个副本,以便下次可以在新副本上绘制。

    【讨论】:

    • 感谢 Ken,只需添加 NSImage *originalImage = [NSImage imageNamed:@"Drag-Icon.png"]; NSImage *dragImage = [originalImage 复制];工作。那么, -lockFocus 实际上锁定了底层文件吗?或者至少是它的“便签本”版本?
    • 是的。 +[NSImage imageNamed:] 显然是第一次从文件中获取图像,然后将其缓存并在后续调用中返回相同的 NSImage。
    猜你喜欢
    • 1970-01-01
    • 2010-09-12
    • 2016-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多