stretch a Image like android nine-patch?
Asked Answered
B

5

12

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/

https://static.mcmap.net/file/mcmap/ZG-AbGLDKwfpKnMxcF_AZVLQamyA/Rc6my.png

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

Thanks in advance.

Borges answered 25/10, 2011 at 7:45 Comment(3)
Why are you not using the nine-patch-loader-for-iphoneSuperfluous
there may have a better solution,like a uiimage category,sth like this: - (UIImage *)imageWithStretchableRect:(CGRect)rect destRect:(CGRect)destRectBorges
i use this lib,and the affect is not what i want . i want to repeat some part of the image .Borges
I
20

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.

Intendment answered 11/10, 2012 at 14:53 Comment(0)
K
4

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];
Kepner answered 26/12, 2012 at 14:35 Comment(0)
G
2

As of iOS5, you can use resizableImageWithCapInsets: to achieve this.

Granth answered 28/11, 2011 at 7:2 Comment(0)
F
1

I didn't want to load so much code for the simple 9 patch images we had. So anyway, here's a simple solution to the problem for those interested. It's somewhat hard coded to our 3 pixel stretch regions but you could easily adjust those to your needs or find those in the image with some basic code and use that instead of my hard-coded inset parameters.

+ (UIImage *) makeNinePatchImage:(UIImage *)image;
{
    //Clear the black 9patch regions
    CGRect imageRect = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
    UIGraphicsBeginImageContextWithOptions(image.size, NO, [UIScreen mainScreen].scale);
    [image drawInRect:imageRect];

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextClearRect(context, CGRectMake(0, 0, image.size.width, 1));
    CGContextClearRect(context, CGRectMake(0, 0, 1, image.size.height));

    image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    UIEdgeInsets insets;

    //hard coded for now, could easily read the black regions if necessary
    insets.left = insets.right = image.size.width / 2 - 1;
    insets.top = insets.bottom = image.size.height / 2 - 1;

    UIImage *nineImage = [image resizableImageWithCapInsets:insets     
                          resizingMode:UIImageResizingModeStretch];

    return nineImage;

}

Then to use it in say, a button's background, just call it like this:

[self.dummyButton setBackgroundImage:[EVUtility makeNinePatchImage:learnMoreImage] forState:UIControlStateNormal];

Done.

Futile answered 9/9, 2013 at 23:17 Comment(0)
S
0

If I understand it right, you want the rectangle to grow in length and height. While there may be a more efficient solutions, what I could suggest is to break the boundary into a basic unit. Add this image repeatedly one next to the other to form the border rectangle of desired length and height. Then paint the inner rect with gray color.

Superfluous answered 27/10, 2011 at 15:0 Comment(1)
yes,exactly.i just want to repeat only part of image #7901559 thanks for you reply. your answer did help meBorges

© 2022 - 2024 — McMap. All rights reserved.