views:

211

answers:

2

In questions like this one it shows how to use UIKit's

-(UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight

to stretch a PNG easily, e.g when inserting text in an iChat like bubble. I would like to do the same on regular AppKit/Cocoa/desktop app using CoreGraphics or some NSImage related API but couldn't find an equivalent method. Does anybody know how to do this in AppKit?

+1  A: 

Just use -[NSImage drawInRect:fromRect:operation:fraction:], and pass NSZeroRect as the fromRect; parameter.

Ben Gottlieb
Does this work to smoothy stretch/tile an image along one axis? I tried this with a png and it didn't work, it just stretched all the pixels. I know there must be some way to do this with one png because I notice that a lot of apps have single pngs in their resources. NSDrawThreePartImage did work however.
John Wright
Btw, I called this as follows: [speechBubbleImage drawInRect:speechBubbleRect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
John Wright
+2  A: 

Use NSDrawThreePartImage or NSDrawNinePartImage.

Peter Hosey