2010-05-19 189 views
1

我想设置一个自定义的拖动图标在NSTableView中使用。一切似乎都奏效,但由于我对Quartz缺乏经验,我遇到了一个问题。NSImage透明度

- (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呈现低透明度,但不是我的图标。

回答

3

问题是你正在绘制自己的图标。你可能想要的是这样的:

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

这样做更有意义。谢谢。 – ndg 2010-05-20 11:29:52

+4

此外,['compositeToPoint:fromRect:operation:fraction:'is deprecated](http://developer.apple.com/mac/library/documentation/Cocoa/Reference/ApplicationKit/Classes/NSImage_Class/DeprecationAppendix/AppendixADeprecatedAPI.html# // apple_ref/OCC/instm/NSImage中/ compositeToPoint:fromRect:操作:分数:)。改用'drawAtPoint:fromRect:operation:fraction:'代替。 – 2010-05-21 10:10:34

+0

好抓;我已经把这个消除了。 – Wevah 2010-05-21 13:29:11