Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stretch a Image like android nine-patch?

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.

like image 378
guoxj Avatar asked Oct 25 '11 07:10

guoxj


People also ask

How do I create a 9 patch image?

In Android Studio, right-click the PNG image you'd like to create a NinePatch image from, then click Create 9-patch file. Type a file name for your NinePatch image, and click OK. Your image will be created with the . 9.

What is the difference between a regular .png and a nine patch image?

The nine patch images have extension as. 9. png. It allows extension in 9 ways, i.e. 4 corners that are unscaled, 4 edges that are scaled in 1 axis, and the middle one that can be scaled into both axes.

How do I create a 9 patch image in Photoshop?

Creating 9 patch background image using Photoshop:Draw the images in a regular way as you use to do in Photoshop. Make the 1px spacing around the image from all sides. And also make sure that the image doesn't have anything in this 1px area & has 100% transparency. Otherwise 9patch image will not work.

What is a NinePatch?

A 9 patch image is a regular png (. PNG) image which is needful for android app developers where they require to wrap any content within a background image without pixelating the background image.


2 Answers

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.

like image 63
aToz Avatar answered Oct 12 '22 12:10

aToz


You can achieve nine patch with Tortuga22 software

You can find source from this GitHub here

Use it like this

[btnSubmit setImage: [TUNinePatchCache imageOfSize:[btnSubmit bounds].size 
                                forNinePatchNamed:@"imgNormalBackground"]
                       forControlState:UIControlStateNormal];
like image 44
Azhar Avatar answered Oct 12 '22 13:10

Azhar