On or about 12/6/09 8:11 PM, thus spake "[email protected]"
<[email protected]>:

> Date: Fri, 4 Dec 2009 14:22:04 -0700
> From: John Wright <[email protected]>
> Subject: Draw rounded NSImage
> 
> I am trying to create a NSImage or NSImageCell with rounded corners
> inside a NSTableView. I can't get anything to work. Here is the best I
> have so far inside my custom NSCell:
> 
> - (void)drawInteriorWithFrame:(NSRect)frame inView:(NSView *)controlView {
>   if (thumbnailLink) {
>         NSURL *url = [NSURL URLWithString:thumbnailLink];
>         if (url) {
>                 NSRect imageFrame = [self _imageFrameForInteriorFrame:frame];
>                 NSImage *image = [[NSImage alloc] initWithContentsOfURL:url];
>                 [image setScalesWhenResized:YES];
>                 [image setSize:NSMakeSize(IMAGE_HEIGHT, IMAGE_WIDTH)];
> 
>                 [NSGraphicsContext saveGraphicsState];
>                 imageFrame = NSInsetRect(imageFrame, 1, 1);
>                 NSBezierPath *clipPath = [NSBezierPath
> bezierPathWithRoundedRect:imageFrame cornerRadius:5.0];
>                 [clipPath setWindingRule:NSEvenOddWindingRule];
>                 [clipPath addClip];
>                 [NSGraphicsContext restoreGraphicsState];
>                 [image drawInRect:imageFrame fromRect:NSMakeRect(0, 0,
> 0, 0) operation:NSCompositeSourceIn fraction:1.0];
>                 [image release];
>         }
> }

You're clipping in a different graphics context from the one you draw the
image in, so the clipping doesn't affect the image. Look at the clipping
example on this page:

http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CocoaD
rawingGuide/GraphicsContexts/GraphicsContexts.html

Notice how the isolation of the graphics context surrounds both the clipping
and the image drawing.

In your example, though, since the clipping and the image are all the
drawing you're doing, there's no need to isolate a graphics context in any
case.

m.

-- 
matt neuburg, phd = [email protected], http://www.tidbits.com/matt/
pantes anthropoi tou eidenai oregontai phusei
Among the 2007 MacTech Top 25, http://tinyurl.com/2rh4pf
AppleScript: the Definitive Guide, 2nd edition
http://www.tidbits.com/matt/default.html#applescriptthings
Take Control of Exploring & Customizing Snow Leopard
http://tinyurl.com/kufyy8
RubyFrontier! http://www.apeth.com/RubyFrontierDocs/default.html
TidBITS, Mac news and reviews since 1990, http://www.tidbits.com



_______________________________________________

Cocoa-dev mailing list ([email protected])

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to