Drawing transparent NSImage makes all NSViews transparent
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.
开发者_StackOverflow中文版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.
[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 ofimg
.
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.
精彩评论