我有一个应用程序,它有多个按钮,这些按钮都是在界面构建器中分配的图像。然后我在应用程序中更改图像。我想知道你将如何将图像更改回原始图像....我通过以下代码完成它但它不断崩溃应用程序
- (IBAction)resetCards
{
NSString *cardImage = [[NSString alloc] initWithFormat:@"CardBackground.png"];
UIImage *buttonImage = [UIImage imageNamed:cardImage];
[holdCardOne setImage:buttonImage forState:UIControlStateNormal];
[holdCardTwo setImage:buttonImage forState:UIControlStateNormal];
[flopCardOne setImage:buttonImage forState:UIControlStateNormal];
[flopCardTwo setImage:buttonImage forState:UIControlStateNormal];
[flopCardThree setImage:buttonImage forState:UIControlStateNormal];
[turnCard setImage:buttonImage forState:UIControlStateNormal];
[riverCard setImage:buttonImage forState:UIControlStateNormal];
[cardImage release];
[buttonImage release];
}
答案 0 :(得分:3)
不要致电[buttonImage release]
- 它正在自动释放。
通常只有在调用alloc,copy或retain时才调用release。如果您不确定,请务必查看文档以获取返回对象的消息,但正常的Objective-C过程是返回自动重新发布的对象。
答案 1 :(得分:1)
除了Lou的回答,为什么不使用:
UIImage *buttonImage = [UIImage imageNamed:@"cardBackground.png"];
而不是创建NSString?