Is there a nine-patch loader for iPhone?

n8gray picture n8gray · Jul 16, 2009 · Viewed 10.5k times · Source

Android has a nice way of defining stretchable images called a nine-patch. See these docs for a description of the concept. The idea is to surround a png image with a 1-pixel border where you can define the stretchable areas and the padding dimensions of the image. This is absolutely brilliant and I'd like to use the idea in my iPhone app. Before writing my own nine-patch to UIImage loader I thought I'd see if one already exists. Google doesn't return any results so I don't have much hope, but it doesn't hurt to ask, right? :-)

EDIT: Folks, I appreciate the answers but I know about stretchableImageWithLeftCapWidth.... I'm looking for code that takes a path @"foo.9.png" and returns a stretchable UIImage. This code will undoubtedly use stretchableImageWithLeftCapWidth... internally. I'm sure I could write the code myself using that method. But I'm asking if somebody else has already done it.

Answer

n8gray picture n8gray · Jun 2, 2010

I received an e-mail from Tortuga22 software who informed me that they have created such a library and released it under the Apache license:

Announcement: http://blog.tortuga22.com/2010/05/31/announcing-tortuga-22-ninepatch/

Source code: http://github.com/tortuga22/Tortuga22-NinePatch

Example usage:

// loads-and-caches ninepatch and rendered image of requested size
UIImage buttonImg = [TUNinePatchCache imageOfSize:buttonSize 
                                forNinePatchNamed:@"buttonNormalBackground"];
[self.buttonNeedingBackground setImage:buttonImg
                       forControlState:UIControlStateNormal];