在OSX上显示带alpha通道的图像

时间:2016-05-15 10:17:45

标签: image macos alpha

我正在尝试在窗口中显示32位,4通道图像。图像中的第四个通道是alpha通道。作为实验,我创建一个完全红色的图像,其中每个像素具有RGB值:255,0,0。对于每个像素,我还添加了一个204的alpha值。

我期望看到的是具有一定透明度的完全红色图像,但我看到的是一个完全不透明的图像,其值已经改变。

预期产出: enter image description here

当前输出: enter image description here

我正在使用的代码:

NSRect windowRect = {0,0,200,200};

m_NSWindow = [[NSWindow alloc] initWithContentRect:windowRect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO];

[m_NSWindow setTitle:@"overlayWindow"];

[m_NSWindow makeKeyAndOrderFront:nil];

g_imageView = [[NSImageView alloc] initWithFrame:NSMakeRect(0,0,200,200)];

[m_NSWindow.contentView addSubview:g_imageView];



[m_NSWindow setOpaque:NO];

[m_NSWindow setAlphaValue:1.0];



NSBitmapImageRep* imageRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:nil

                                                                     pixelsWide:200

                                                                     pixelsHigh:200

                                                                  bitsPerSample:8

                                                                samplesPerPixel:4

                                                                       hasAlpha:YES

                                                                       isPlanar:NO

                                                                 colorSpaceName:NSDeviceRGBColorSpace

                                                                bitmapFormat:NSAlphaNonpremultipliedBitmapFormat

                                                                    bytesPerRow:(200*4)

                                                                   bitsPerPixel:32];



memcpy(imageRep.bitmapData,m_paintBuffer.data,160000);

NSSize imageSize = NSMakeSize(200,200);

NSImage* myImage = [[NSImage alloc] initWithSize: imageSize];

[myImage addRepresentation:imageRep];

[g_imageView setImage:myImage];

其中m_paintBuffer.data指向图像的原始像素数据。 不确定它是否与问题相关,但m_paintBuffer属于OpenCV中的Mat类型

1 个答案:

答案 0 :(得分:1)

窗口的内容视图不是窗口内容的全部内容。即使是无边框窗口也有(私有)主题视图,包含内容视图。

特别是,窗口的背景颜色为浅灰色。这是在您的内容视图“后面”绘制的,并且是不透明的。但是,您可以通过将窗口的backgroundColor属性设置为[NSColor clearColor]来更改它。这就是你应该做的,以达到你想要的效果。