Search code examples
iphoneiosmemoryuibuttoncore-animation

iOS5 UIButton should I try to optimize background image?


I got up to 16 buttons on a single UIViewController. Each button has a custom style with a background png image measuring 160x160 pixels (scaled down for retina display). The buttons themselves are 40x40, 60x60, or 80x80. I expect the view controller to live for a very long time.

There are only 3 types of images that I want to assign to these buttons (red, yellow, green). The plan is to re-assign images to indicate state transitions. Currently my UI is built in interface builder. Is the Interface Builder smart enough to realize that I'm trying to re-use the same image for these buttons, or will it allocate 16 separate images, one for each button? Something tells me that each button has a separate UILayer which somehow gets the image.

Thank you!


Solution

  • They all use the same image. You can easily test this by making a subclass of UIButton with this method:

    - (void)awakeFromNib
    {
        NSLog(@"TestButton %p - background image %p %@", self, self.currentBackgroundImage, self.currentBackgroundImage);
    }
    

    Set your buttons to your UIButton subclass, run, and check the output:

    2011-11-06 13:36:41.877 tester[21909:f803] TestButton 0x68522a0 - background image <UIImage: 0xed2d630>
    2011-11-06 13:36:41.878 tester[21909:f803] TestButton 0xed2f1e0 - background image <UIImage: 0xed2d630>
    2011-11-06 13:36:41.879 tester[21909:f803] TestButton 0xed2f6a0 - background image <UIImage: 0xed2d630>