希望有人可以在这里帮助我,因为我失去了忍受这个记忆问题的意愿。 当我推动一个特定的视图控制器时,Instruments显示大约3MB的增长,其中大部分来自属于我在视图控制器上的每个控件的CALayer对象(主要是文本字段和标签)。 当我弹出这个视图控制器时,这些CALayer分配保留在内存中,实际上每次我按下并弹出控制器时,每次都会增加3MB。
所以我的问题是如何从内存中删除这些内容,在解除分配控制器时我需要做些什么?我的代码中唯一引用控件'图层是创建圆角和边框宽度(layer.cornerRadius = 10),但即使我发表评论,我仍然可以获得增长。 控制器包含一个tableview,其中每个单元格包含3个文本字段,2个按钮和一个以编程方式创建的imageview。
-(UIButton*) formatButton:(UIButton*)button
{
button.layer.cornerRadius = 10;
button.clipsToBounds = YES;
button.layer.borderColor=[[UIColor lightGrayColor] CGColor];
button.layer.borderWidth=1.0;
[button.titleLabel setTextAlignment:NSTextAlignmentCenter];
[button.titleLabel setFont:[UIFont fontWithName:@"HelveticaNeue-UltraLight" size:20]];
[button setTitle:@"0" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
return button;
}
-(UITextField*)formatTextField:(UITextField*)textField
{
[textField setFont:[UIFont fontWithName:@"HelveticaNeue-UltraLight" size:20]];
textField.backgroundColor=[UIColor whiteColor];
textField.layer.borderColor=[[UIColor lightGrayColor] CGColor];
textField.layer.cornerRadius = 10;
textField.layer.borderWidth=1.0;
[textField setTextAlignment:NSTextAlignmentCenter];
textField.clipsToBounds=YES;
textField.text=@"";
textField.enabled=NO;
return textField;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ItemCellID" forIndexPath:indexPath];
// Configure the cell...
OrderedProduct *orderedProduct=self.orderedProductsArray[indexPath.row];
//NSLog (@"ordered Product %@",orderedProduct.productID);
UITextField *qualityField=[[UITextField alloc] initWithFrame:CGRectMake(113,23,324,30)];
qualityField=[self formatTextField:qualityField];
[cell addSubview:qualityField];
UITextField *sizeField=[[UITextField alloc] initWithFrame:CGRectMake(445, 24, 142, 30)];
sizeField=[self formatTextField:sizeField];
[cell addSubview:sizeField];
UITextField *totalPriceField=[[UITextField alloc] initWithFrame:CGRectMake(786,24,137,30)];
totalPriceField=[self formatTextField:totalPriceField];
[cell addSubview:totalPriceField];
UIButton *quantityButton=[[UIButton alloc] initWithFrame:CGRectMake(595,24,60,30)];
quantityButton=[self formatButton:quantityButton];
[cell addSubview:quantityButton];
UIButton *priceButton=[[UIButton alloc] initWithFrame:CGRectMake(663,24,119,30)];
priceButton=[self formatButton:priceButton];
[cell addSubview:priceButton];
UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(5,15,100,50)];
[cell addSubview:imageView];
UIButton *removeButton=(UIButton*)[cell viewWithTag:kRemoveButton];
if (self.selectedOrder.status==[NSNumber numberWithInt:kOrderStatusProvisional])
{
quantityButton.enabled=YES;
quantityButton.backgroundColor=[Settings getInstance].enabledButtonColor;
[quantityButton addTarget:self action:@selector(quantityButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
priceButton.enabled=YES;
priceButton.backgroundColor=[Settings getInstance].enabledButtonColor;
removeButton.hidden=NO;
[removeButton addTarget:self action:@selector(removeButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
}
else
{
quantityButton.enabled=NO;
quantityButton.backgroundColor=[Settings getInstance].disabledButtonColor;
priceButton.enabled=NO;
priceButton.backgroundColor=[Settings getInstance].disabledButtonColor;
removeButton.hidden=YES;
}
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setCurrencyCode:@"GBP"];
[numberFormatter setNumberStyle: NSNumberFormatterCurrencyStyle];
imageView.image=[self.imageCache objectForKey:orderedProduct.product.productImageName];
UITapGestureRecognizer *tgr=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageViewTapped:)];
imageView.userInteractionEnabled=YES;
[imageView addGestureRecognizer:tgr];
qualityField.text=orderedProduct.product.name;
sizeField.text=orderedProduct.product.productSize.productSize;
quantityButton.titleLabel.text=[NSString stringWithFormat:@"%@",orderedProduct.quantity];
[quantityButton setTitle:[NSString stringWithFormat:@"%@",orderedProduct.quantity] forState:UIControlStateNormal];
float price=[orderedProduct.price integerValue]/100; // server price is in pennies
float totalPrice=price*[orderedProduct.quantity integerValue];
[priceButton setTitle:[numberFormatter stringFromNumber:[NSNumber numberWithFloat:price]] forState:UIControlStateNormal];
totalPriceField.text=[numberFormatter stringFromNumber:[NSNumber numberWithFloat:totalPrice]];
return cell;
}
无法发布图像,所以下面是对乐器所说内容的粗略描述,
快照增长
B代3.64MB
VM:UILabel(CALayer)964KB
VM:UITextFieldLabel(CALayer)792KB
VM:CoreAnimation 788KB