Search code examples
iosios5

resizableImage for UIImageView


I am working for IOS 5.0 as minTarget

I have a UIImageView to which i want to assign a resizable image, so that image don't get stretch from corners.

i have tried setting content mode of UIImageView to UIViewContentModeScaleToFill. But the image appears as tiled.

here's the code

UIImage *bgImage = [[UIImage imageNamed:@"ImgViewBg"] resizableImageWithCapInsets:UIEdgeInsetsMake(2, 2, 2, 2)];

imgView.contentMode = UIViewContentModeScaleToFill;
imgView.image = bgImage;

I am looking for the same effect as we have with 9patch images in android

here's the image i am trying on ImgViewBg

I just gave an another look at the documentation of resizeableImageWithCapInsets. It says it tiles the the area which is not under cap. I think that what causing the tiled pattern. Is there any workaround to this so that i can have 9Patch style image??

EDIT

According to Apple Docs
upto IOS 5.0 following works for my req.
[[UIImage imageNamed:@"textViewBg"] stretchableImageWithLeftCapWidth:6 topCapHeight:6]; (as mentioned by Dipen Panchasara)

IOS6.0 and later following works for my req.
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode

IOS 5.0 and later (which i required)
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets
above function does not work in my case, as it tiles the image not under cap.

so for IOS 5.0 to IOS 6.0 i was not able to find anything which solves my requirement.

For now i moving to use Dipen Panchasara solution, i hope it stays stable


Solution

  • // Use following code to make stretchableBackground
    UIImage *bgImage = [[UIImage imageNamed:@"ImgViewBg"] stretchableImageWithLeftCapWidth:13 topCapHeight:13];
    [imgView setImage:bgImage];