我有一些案例为每个案例生成了不同的图像。问题是,一旦生成图像,它们就会堆叠起来。如何设置每一种情况来覆盖前面的图像?
- (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"]];
}
}更新的代码:所以这个程序每次用initWithImage创建一个新的映像。我现在将返回设置为只返回UIImage。为什么这段代码也不修复这个问题呢?
- (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
}
}发布于 2014-03-26 08:30:52
请阅读我上面的评论。然后将您的setTitleCode修改为如下所示。
- (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;
}发布于 2014-03-26 08:05:43
您每次都要创建一个新的图像视图并添加它。最终,这可能会导致您的应用程序崩溃,因为内存的使用(吨的图像查看可能是昂贵的)。
使图像视图一次,并相应地设置其图像(只需从switch语句中返回图像),或者在添加新图像之前删除前面的图像视图。
您需要在某个地方保留对图像视图的引用,理想情况下,它是任何类上的属性。然后,只需设置它的图像属性。这里不要创建新的图像视图,也不要添加任何子视图。图像视图应该在init方法中创建一次。
发布于 2014-03-26 08:09:01
如果您的意思是在开关之后创建多个图像,那么尝试使用
break();但是安全的事情是创建一个imageView并插入它,然后在开关语句中设置它的图像,这样即使它被放置了不止一个,它也会保留最后的引用。
https://stackoverflow.com/questions/22654744
复制相似问题