保留、重用、释放?
我有一系列按钮,每个按钮都使用不同的图像。我可以重用如下所示的保留变量吗:
// 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];
还是需要为传递给每个按钮的 setBackgroundImage:
的每个图像创建一个新的 UIImage
并依赖自动释放而不是保留UIImage。上面的方法有效,但我不确定将每个按钮的图像设置为同一图像将如何影响保留计数。
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:
and rely on autorelease rather than a retained UIImage. The above works but I'm not sure how setting the image for each of these buttons to the same image will effect the retain counts.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你所拥有的将会泄漏(因为你失去了对第一个图像的引用)并崩溃(因为你
释放
一个不属于你的对象)。如果删除-retain
和-release
,您的代码将正常工作。What you have there will leak (because you lose the reference to the first image) and crash (because you
release
an object you do not own). If you remove the-retain
and the-release
, your code will work just fine.