Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do [UIImage resizableImageWithCapInsets:] prior to iOS5?

Tags:

ios

uiimage

ios5

The following code tiles the image area within the specified insets:

UIEdgeInsets imgInsets = UIEdgeInsetsMake(10.f, 5.f, 13.f, 44.f);
UIImage *image = [[UIImage imageNamed:@"fileName"] resizableImageWithCapInsets:imgInsets]; 

However this is only available in iOS5. How can I achieve the same result for before-iOS5 compatibility?

[UIImage stretchableImageWithLeftCapWidth: topCapHeight:] is not appropriate as far as I understand, because it assumes that the tile-able area is 1px wide. In other words it doesn't tile, it streches. Therefore it doesn't work with patterns, only with single-color images. This is demonstrated in the screenshot below.

Then there is [UIColor colorWithPatternImage:], but this assumes that the entire image needs to be tiled, it doesn't allow for insets that must remain capped.

Any help appreciated, thanks.

.

enter image description here

like image 626
DTs Avatar asked Dec 01 '11 01:12

DTs


1 Answers

I've been looking for a solution to this too. At this point I think I'll use respondsToSelector:@selector(resizableImageWithCapInsets:) on the original UIImage to see if the method is available. If not, then use stretchable image.

I'm still looking for a better solution, and if one comes up, I'll update the answer.

like image 158
MechEngineer Avatar answered Jan 05 '23 07:01

MechEngineer