NSImage 透明度

发布于 2024-09-02 06:43:07 字数 734 浏览 4 评论 0原文

我正在尝试设置一个自定义拖动图标以在 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;
}

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

I'm trying to set a custom drag icon for use in an NSTableView. Everything seems to work but I've run 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;
}

Essentially what I'm looking to do is render my icon.png file with 50% opacity along with an NSString which shows the number of rows currently being dragged. The issue I'm seeing is that my NSString renders with a low opacity, but not my icon.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

囚我心虐我身 2024-09-09 06:43:07

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

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

The issue is that you’re drawing your icon on top of itself. 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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文