Re: Draw rounded NSImage
Re: Draw rounded NSImage
- Subject: Re: Draw rounded NSImage
- From: Matt Neuburg <email@hidden>
- Date: Sun, 06 Dec 2009 20:29:26 -0800
- Thread-topic: Draw rounded NSImage
On or about 12/6/09 8:11 PM, thus spake "email@hidden"
<email@hidden>:
> Date: Fri, 4 Dec 2009 14:22:04 -0700
> From: John Wright <email@hidden>
> 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@hidden, 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@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