Re: NSImage change of behaviour?
Re: NSImage change of behaviour?
- Subject: Re: NSImage change of behaviour?
- From: Ken Ferry <email@hidden>
- Date: Mon, 8 Mar 2010 20:53:54 -0800
Hi Graham,
I don't think the problem is exactly what is described here. A test app
behaves as expected. <http://homepage.mac.com/kenferry/temp/ImageTest.zip>.
However, I could believe that what your seeing does have something to do
with locking focus on an image and then drawing from that image into itself.
Quoth the release notes:
"Another issue we've seen is with clients calling -lockFocus on an image,
then drawing the image elsewhere before calling -unlockFocus. This no longer
works. The NSImage itself is not modified until -unlockFocus is called, as
described above."
That is, in 10.6, you're drawing the image as it was prior to the
-lockFocus. It's sort of like a double buffer.
Drawing an image while focus is locked on it should be avoided. There are
probably lots of ways to get the effect your app wants (most more readable
than the following), but a direct way to halve the alpha channel is to draw
with DestinationIn mode.
NSCompositeDestinationIn
Destination image wherever both images are opaque, and transparent
elsewhere. (R = D*Sa)
Result = Destination * source alpha. Destination is the color that is
already there, source is the new color you're drawing.
[[[NSColor clearColor] colorWithAlphaComponent:0.5] set];
NSRectFillUsingOperation(imBounds, NSCompositeDestinationIn);
-Ken
On Mon, Mar 8, 2010 at 6:25 PM, Graham Cox <email@hidden> wrote:
> Not sure if this is a bug or not, but it's certainly something that seems
> to have changed and I can find no mention of it in the release notes or
> elsewhere.
>
> Typically when making an image for use as a drag image, I draw a bunch of
> things into an image, then copy it to itself with 50% alpha. This has always
> worked fine, using the following general approach:
>
> NSImage* image = [[NSImage alloc] initWithSize:]
>
> [image lockFocus];
>
> // ... draw a bunch of stuff ...
>
> // copy to self at 50% opacity
> [image drawAtPoint:NSZeroPoint fromRect:NSZeroRect
> operation:NSCompositeCopy fraction:0.5];
> [image unlockFocus];
>
>
> I'm now finding that I don't get the semi-transparent image, but just a
> fully opaque image. I can workaround it like so:
>
> NSImage* image = [[NSImage alloc] initWithSize:]
>
> [image lockFocus];
>
> // ... draw a bunch of stuff ...
>
> [image unlockFocus];
>
> [image lockFocus];
> [image drawAtPoint:NSZeroPoint fromRect:NSZeroRect
> operation:NSCompositeCopy fraction:0.5];
> [image unlockFocus];
>
>
> So the extra unlock/lock seems to trigger some internal change that allows
> the transparent effect to work, but that wasn't needed in the past.
>
> Anyone care to comment? Maybe the old way was always broken and it worked
> by chance, or maybe a change to image functionality didn't anticipate this
> usage pattern...
>
> --Graham
>
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden