Retain, reuse, release?
- by Typeoneerror
I've got a series of buttons that each use a different image. Can I reuse a retained variable like this below:
// set images
UIImage *image = [[dice1 backgroundImageForState:UIControlStateHighlighted] retain];
[dice1 setBackgroundImage:image forState:(UIControlStateHighlighted|UIControlStateSelected)];
image = [dice2 backgroundImageForState:UIControlStateHighlighted];
[dice2 setBackgroundImage:image forState:(UIControlStateHighlighted|UIControlStateSelected)];
image = [dice3 backgroundImageForState:UIControlStateHighlighted];
[dice3 setBackgroundImage:image forState:(UIControlStateHighlighted|UIControlStateSelected)];
image = [dice4 backgroundImageForState:UIControlStateHighlighted];
[dice4 setBackgroundImage:image forState:(UIControlStateHighlighted|UIControlStateSelected)];
image = [dice5 backgroundImageForState:UIControlStateHighlighted];
[dice5 setBackgroundImage:image forState:(UIControlStateHighlighted|UIControlStateSelected)];
image = [dice6 backgroundImageForState:UIControlStateHighlighted];
[dice6 setBackgroundImage:image forState:(UIControlStateHighlighted|UIControlStateSelected)];
[image release];
or do I need to create a new UIImage for each image passed to each button's setBackgroundImage: like so:
// set images
UIImage *image1 = [dice1 backgroundImageForState:UIControlStateHighlighted];
[dice1 setBackgroundImage:image1 forState:(UIControlStateHighlighted|UIControlStateSelected)];
UIImage *image2 = [dice2 backgroundImageForState:UIControlStateHighlighted];
[dice2 setBackgroundImage:image forState:(UIControlStateHighlighted|UIControlStateSelected)];
and rely on autorelease rather than a retained UIImage. I'm not sure if assigning the image to a different UIImage would effect the retain count.