绘制透明 NSImage 使所有 NSView 透明

发布于 2024-10-15 06:59:12 字数 847 浏览 3 评论 0原文

我在 Cocoa 应用程序中有三个 NSView,每个视图都是另一个视图的子视图。

首先NSView ->第二个NSView -> ThirdNSView

FirstNSView 使用核心图形绘制纯色背景填充。

SecondNSView 是许多其他视图的容器视图。

ThirdNSView 使用 drawRect 中的以下代码将 NSImage 绘制到其框架中:

[img drawAtPoint:NSMakePoint(0, 0) fromRect:NSMakeRect(0, 0, img.size.width, img. size.height) 操作:NSCompositeDestinationAtopfraction:1.0];

我使用 NSCompositeDestinationAtop 因为它允许图像几乎充当 img 的非透明区域的遮罩。代码>.

然而,img 的透明区域使整个窗口透明,而不是像我期望的那样显示到 FirstNSView。像这样:

在此处输入图像描述

请注意,尝试其他复合类型仍然会导致同样的问题。

我读过很多关于 NSViews 不适合“分层”的文章,但我不确定这是否与这里相关。到目前为止,我已经使用 Core Graphics 在 NSView 上进行了很好的绘图,并且透明部分暴露了“后面”的层。只是在尝试合成该图像时才出现此问题。

我将不胜感激任何关于为什么会发生这种情况以及解决它的可能方法的指示。

I have three NSViews in a Cocoa application, each subviews of the other.

FirstNSView -> SecondNSView -> ThirdNSView

FirstNSView draws a solid background fill using core graphics.

SecondNSView is a container view for a number of other views.

ThirdNSView draws an NSImage into its frame using the following code in drawRect:

[img drawAtPoint:NSMakePoint(0, 0) fromRect:NSMakeRect(0, 0, img.size.width, img.size.height) operation:NSCompositeDestinationAtop fraction:1.0];

I am using NSCompositeDestinationAtop because it allows the image to almost act as a mask for the non transparent region of img.

However, the transparent regions of img make the whole window transparent, rather than showing through to FirstNSView as I would expect. Like so:

enter image description here

Note that experimenting with other composite types has still lead to this same issue.

I have read alot about NSViews not being meant for 'layering' but I'm unsure if that is relevant here. Up to now I've been fine drawing using Core Graphics onto an NSView and the transparent sections exposing the layer 'behind'. It's only with trying to composite this image that this problem has arisen.

I'd appreciate any pointers as to why this is happening and a possible way of solving it.

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

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

发布评论

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

评论(1

朮生 2024-10-22 06:59:12
[img drawAtPoint:NSMakePoint(0, 0) fromRect:NSMakeRect(0, 0, img.size.width, img.size.height) 操作:NSCompositeDestinationAtopfraction:1.0];

我使用 NSCompositeDestinationAtop 因为它允许图像几乎充当 img 的非透明区域的遮罩。

哪个图像?如果您想使用 img 的 Alpha 通道作为遮罩,那就是 NSCompositeSourceOver

DestinationAtop 适用于您希望图像出现在窗口下方的情况,因此您应该通过窗口中任何已经透明的区域看到图像。

但是,img 的透明区域使整个窗口透明,而不是像我期望的那样显示到 FirstNSView。就像这样:

[截图]

我无法从该屏幕截图中看出任何内容。对我来说,它看起来就像一堆随机的彩色形状。

我读过很多关于 NSViews 不适合“分层”的文章,但我不确定这是否与这里相关。

事实并非如此。重叠的同级视图会导致问题。

[img drawAtPoint:NSMakePoint(0, 0) fromRect:NSMakeRect(0, 0, img.size.width, img.size.height) operation:NSCompositeDestinationAtop fraction:1.0];

I am using NSCompositeDestinationAtop because it allows the image to almost act as a mask for the non transparent region of img.

Which image? If you want to use img's alpha channel as the mask, that's NSCompositeSourceOver.

DestinationAtop is for when you want the image to appear under the window, so you should see the image through any already-transparent areas in the window.

However, the transparent regions of img make the whole window transparent, rather than showing through to FirstNSView as I would expect. Like so:

[screenshot]

I can't tell anything from that screenshot. It just looks like a bunch of random colored shapes to me.

I have read alot about NSViews not being meant for 'layering' but I'm unsure if that is relevant here.

It isn't. Overlapping sibling views cause problems.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文