Question

I've got a load of images that are resizable in my app and I use them all over the place.

Most of them have the same edge insets but some are different.

It takes a lot of code to create them too...

UIImage *buttonBGImage = [[UIImage imageNamed:@"buttonBG"] resizableImageWithCapInsets:UIEdgeInsetsMake(4, 4, 5, 4) resizingMode:UIImageResizingModeStretch];

It's only one line of code but I have to go looking for all the edge insets etc...

I'm trying to create a convenience method for it instead. Something like...

UIImage *buttonBGImage = [blah resizableButtonBG];

Where would be the best place for these? I could create a singleton called "ImageManager". That way each resizable image would only get instantiated once and then would be held in memory. Is that overkill?

I could add a category to UIImage called...

+(UIImage *)resizableButtonBG;

Can anyone provide any advice on this? Is my singleton idea a good one? Are there any cons against using a Singleton?

Was it helpful?

Solution

A category method is definitely a good solution. Many people use this for creating fonts and colors which are used in multiple places in an application.

Using a singleton is not necessary. There is no associated instance data so you might as well stick with simple category methods which return the appropriate image.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top