gpt4 book ai didi

cocoa - 绘制透明 NSImage 使所有 NSView 透明

转载 作者:行者123 更新时间:2023-12-03 17:27:36 38 4
gpt4 key购买 nike

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

FirstNSView -> SecondNSView -> ThirdNSView

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

SecondNSView 是许多其他 View 的容器 View 。

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

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

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

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

enter image description here

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

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

如果您能指出为什么会发生这种情况以及解决该问题的可能方法,我将不胜感激。

最佳答案

[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.

哪个图像?如果你想使用 img 的 Alpha channel 作为 mask ,那就是 NSCompositeSourceOver

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

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 have read alot about NSViews not being meant for 'layering' but I'm unsure if that is relevant here.

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

关于cocoa - 绘制透明 NSImage 使所有 NSView 透明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4828509/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com