Retain, reuse, release?

Posted by Typeoneerror on Stack Overflow See other posts from Stack Overflow or by Typeoneerror
Published on 2010-04-17T20:30:16Z Indexed on 2010/04/17 20:33 UTC
Read the original article Hit count: 284

Filed under:
|

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.

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about retain