I would like to create my own subclass of UIImage that would allow me to create a realizable/stretchable image with caps on each of the four sides.
iOS 5’s resizableImage does not fit my needs since it does not allow me to stretch the middle part, neither does the now deprecated stretchableImage because it allows me to repeat only a single pixel, while I may want to repeat some sort of gradient.
So far everything works fine, I implemented my own drawing code in -drawInRect: and it behaves as expected.
However, when assigning my custom subclass instance as a UIImageView‘s image, my code breaks. The image gets rendered as the normal UIImage would. (scaled in all directions without any caps)
I tried to set breakpoints at all of the four drawing methods (drawInRect, drawAtPoint, both standalone plus with blendMode: but they seem not to get called.
Is there a way to provide a UIImage with custom drawing code when using it within a UIImageView without subclassing UIImageView, too?
Also, I am worrying about performance. The default stretchable image works quite well during animations, but I wonder how it’d be when animating size properties (width & height) of my UIImageView. Would my rendering code get called every frame?
In the Apple Documentation it clearly says you can’t override
-drawRect:of aUIImageView, so subclassingUIImageViewwill not work also. You have to implement your ownUIViewclass.Subclassing
UIImagewill neither work, I have tried this and no public method was called at all, not even the accessory method forCGImage. I think you would need to override some private API to make this work.Don’t worry about resizing, your rendering code will only be called for the final frame.
Still, in most cases performance is worse than using an
UIImageView. Apple optimized the class so it runs on the GPU.