【问题标题】:NSImage transparencyNSImage 透明度
【发布时间】:2011-02-21 10:22:01
【问题描述】:

我正在尝试设置一个自定义拖动图标以在 NSTableView 中使用。一切似乎都正常,但由于我对 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;
}

基本上我想要做的是渲染我的 icon.png 文件,其不透明度为 50% 以及一个 NSString,它显示当前被拖动的行数。我看到的问题是我的 NSString 以低不透明度呈现,但不是我的图标。

【问题讨论】:

    标签: cocoa nsimage


    【解决方案1】:

    问题是您将图标绘制在其自身之上。你可能想要的是这样的:

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

    【讨论】:

    猜你喜欢
    • 2012-04-15
    • 1970-01-01
    • 2013-06-06
    • 1970-01-01
    • 2018-04-02
    • 2020-08-17
    • 1970-01-01
    • 2014-01-05
    • 1970-01-01
    相关资源
    最近更新 更多