Reputation: 29
I have a number of cases that generate a different image for each case. The problem is, the images seem to stack once they are generated. How can I set each case to override the previous image?
- (void)setTileValue:(NSInteger)tileValue {
_tileValue = tileValue;
self.numberLabel.text = [@(tileValue) stringValue];
UIImageView *backgroundView = [self backgroundView:[@(tileValue) intValue]];
[self.numberLabel addSubview:backgroundView];
self.value = tileValue;
}
- (UIImageView *)backgroundView:(NSUInteger)value {
switch (value) {
case 1:
return [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background"]]; // light gray
case 2:
return [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background1"]]; // gray
case 4:
return [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background2"]]; // gark gray
case 8:
return [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background3"]]; // red
case 16:
return [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background4"]]; // orange
default:
return [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background"]];
}
}
UPDATED CODE: So the program is creating a new image each time with the initWithImage. I set the return to only return the UIImage now. Why doesn't this code fix the issue either?
- (void)setTileValue:(NSInteger)tileValue {
_tileValue = tileValue;
self.numberLabel.text = [@(tileValue) stringValue];
// UIImageView *backgroundView = [self backgroundView:[@(tileValue) intValue]];
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[self tileBG:[@(tileValue) intValue]]]; [self.numberLabel addSubview:backgroundView];
self.numberLabel.backgroundColor = [self tileColorForValue:[@(tileValue) intValue]];
self.backgroundColor = [self tileColorForValue:[@(tileValue) intValue]];
self.numberLabel.textColor = [self numberColorForValue:[@(tileValue) intValue]];
self.value = tileValue;
}
- (UIColor *)defaultBackgroundColor {
return [UIColor lightGrayColor];
}
- (UIColor *)defaultNumberColor {
return [UIColor colorWithWhite:0.0 alpha:0.0];
}
- (UIImage *)tileBG:(NSUInteger)value {
switch (value) {
case 1:
return [UIImage imageNamed:@"background"]; // light gray
case 2:
return [UIImage imageNamed:@"background1"]; // gray
case 4:
return [UIImage imageNamed:@"background2"]; // gark gray
case 8:
return [UIImage imageNamed:@"background3"]; // red
case 16:
return [UIImage imageNamed:@"background4"]; // red
case 32:
return [UIImage imageNamed:@"background5"]; // red
default:
return [UIImage imageNamed:@"background"]; // red
}
}
Upvotes: 1
Views: 72
Reputation: 1625
please read my comments above. then modify your setTitleCode
to look like this.
- (void)setTileValue:(NSInteger)tileValue
{
_tileValue = tileValue;
self.numberLabel.text = [@(tileValue) stringValue];
UIImageView *backgroundView = (UIImageView *)[self.numberLabel viewWithTag:1000]; // edited code to avoid warning
if (!backgroundView)
{
backgroundView = [[UIImageView alloc] initWithImage:[self tileBG:[@(tileValue) intValue]]];
backgroundView.tag = 1000;
[self.numberLabel addSubview:backgroundView];
}
backgroundView.image = [self tileBG:[@(tileValue) intValue]];
self.numberLabel.backgroundColor = [self tileColorForValue:[@(tileValue) intValue]];
self.backgroundColor = [self tileColorForValue:[@(tileValue) intValue]];
self.numberLabel.textColor = [self numberColorForValue:[@(tileValue) intValue]];
self.value = tileValue;
}
Upvotes: 1
Reputation: 119282
You're making a new image view every time and adding it. Eventually this could cause your app to crash due to memory use (tons of image views can be expensice).
Make the image view once, and set its image accordingly (just return an image from your switch statement), or remove the previous image view before adding a new one.
You need to keep a reference to the image view somewhere, ideally a property on whatever class this is. Then, just set its image property. Don't create a new image view here, and don't add any subviews. The image view should be created once, in your init method.
Upvotes: 2
Reputation: 4016
If you mean that after the switch it's creating multiple images, then try using a
break();
But the safe things to do it to create an imageView and init it, then set its image in the switch statement, that way even if it gets placed more than one, it will keep the last reference.
Upvotes: 0