绘制透明 NSImage 使所有 NSView 透明
我在 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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
哪个图像?如果您想使用
img
的 Alpha 通道作为遮罩,那就是NSCompositeSourceOver
。DestinationAtop 适用于您希望图像出现在窗口下方的情况,因此您应该通过窗口中任何已经透明的区域看到图像。
我无法从该屏幕截图中看出任何内容。对我来说,它看起来就像一堆随机的彩色形状。
事实并非如此。重叠的同级视图会导致问题。
Which image? If you want to use
img
's alpha channel as the mask, that'sNSCompositeSourceOver
.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.
I can't tell anything from that screenshot. It just looks like a bunch of random colored shapes to me.
It isn't. Overlapping sibling views cause problems.