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.
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With