清除NSImage的alpha通道

问题描述 投票:1回答:2

这可以通过以每个像素32位的方式分配一个临时位图来完成然后使用for循环清除alpha分量,并并再次将其重新转换为NSImage。

我怀疑可以使用聪明的方法以更简单的方式完成NSColor和NSCompositingOperation的组合。或图像需要使用drawAtPoint与其自身进行合成。

我的代码看起来像这样。

NSImage* img = some image with RGB and Alpha;
NSRect rect  = some rect inside the image;
[img lockFocus];
[[NSColor clearColor] set];
NSRectFillUsingOperation(rect, NSCompositeXOR);
[img unlockFocus];

注意:可以通过以下方法将Alpha通道设置为1:使用带有NSCompositePlusLighter的blackColor。

清除Alpha通道的秘密是什么?

cocoa transparency alphablending nsimage
2个回答
4
投票

不会很快,但是也可以:

NSImage *newImage = [[NSImage alloc] initWithSize:[srcImage size]];
[newImage lockFocus];
[[NSColor whiteColor] set];
NSRectFill(NSMakeRect(0,0,[newImage size].width, [newImage size].height));
[srcImage compositeToPoint:NSZeroPoint operation:NSCompositeCopy];
[newImage unlockFocus];

2
投票
  1. 请阅读有关图像可变性的AppKit发行说明。 NSImage基本上应该被视为不可变的。

  2. 所有pixel formats supported in graphics contexts都具有premultiplied alpha。如果Alpha通道为零,则其他通道也必须为零。

© www.soinside.com 2019 - 2024. All rights reserved.