Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom "resizable image" drawing in iOS

The UIImage API provides methods to resize an image in a way that certain areas get stretched, while others remain unaltered - great for background images for resizable UI elements.

Now I am looking for a way to customize this behavior for more complex background images.

This is what - (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets; does. The transparent areas remain unaltered, the red areas are stretched when the view size is changed.

enter image description here

This is what i want - finer grained control over the areas that are stretched:

enter image description here

I guess I'd have to do that by overwriting the -(void)drawRect: method of my UIView subclass and then use Quartz to do the drawing of the image.

I just can't figure out which of the system APIs I would have to use.

Could anybody give me a hint how to accomplish this?

like image 559
de. Avatar asked Nov 21 '13 10:11

de.


1 Answers

I would just use three separate UIImageViews with resizableImageWithCapInsets inside. You could also so it in drawRect using, for example, CGPattern, but you would have performance issue when scaling the view.

like image 77
Alex Watson Avatar answered Sep 27 '22 23:09

Alex Watson