stretch a Image like android nine-patch?

guoxj picture guoxj · Oct 25, 2011 · Viewed 14.9k times · Source

UIImage Method :

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

Here the stretchable area is forced to be a single pixel high/wide.

I can not set the stretchable area, so I want to know: is there a UIImage category to do that?

After I googled, I found a lib.

Question: Is there a nine-patch loader for iPhone?

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

http://i.stack.imgur.com/Rc6my.png

This is a source pic, and I want to stretch the inside gray rect.

Thanks in advance.

Answer

aToz picture aToz · Oct 11, 2012

One way to achieve this is using the below approach,

UIImage * backgroundImg = [UIImage imageNamed:@"bg.png"];

backgroundImg = [backgroundImg resizableImageWithCapInsets:UIEdgeInsetsMake(2,2, 2, 2)];

[imgView setImage: backgroundImg];

UIEdgeInsetsMake(2,2, 2, 2) is the amount of pixels you want unstretchable.