【发布时间】:2016-12-08 15:43:07
【问题描述】:
我有以下创建自定义按钮的函数。每次调用 initKeyboard() 时,都会调用 14 次。在我的应用程序的过程中,用户按下了一个多次调用 initKeyboard 的按钮。每次用户按下按钮时,我都会调用 clearButtonArray()。
我注意到正在使用的内存逐渐增加,当它达到 200MB 左右时,我确实看到我的应用程序的视觉速度有所下降。动画不流畅等
我的问题是,我如何有效地释放 14 个按钮每次使用的内存。看起来 clearButtonArray() 没有做这项工作。
我正在使用 ARC。
感谢您的帮助。
- (void)initKeyboard:(int)scaleNo
{
[self clearButtonArray];
// call createGlideButton 14 times...
}
- (void)createGlideButton:(int)noteVal
string:(NSString *)noteStr
keyMod:(int)key
chromatic:(BOOL)chrOn
x:(int)xPos
y:(int)yPos
{
GlideButton *button = [GlideButton buttonWithType:UIButtonTypeCustom];
[button setTag:noteVal + key];
[button setUserInteractionEnabled:YES];
[button addTarget:self
action:@selector(notePressedDown:withEvent:)
forControlEvents:UIControlEventTouchDown];
[button addTarget:self
action:@selector(notePressedUp:withEvent:)
forControlEvents:UIControlEventTouchUpInside];
[button addTarget:self
action:@selector(notePressedUp:withEvent:)
forControlEvents:UIControlEventTouchUpOutside];
[button addTarget:self
action:@selector(notePressedUp:withEvent:)
forControlEvents:UIControlEventTouchDragExit];
[button addTarget:self
action:@selector(notePressedUp:withEvent:)
forControlEvents:UIControlEventTouchCancel];
[button addTarget:self
action:@selector(notePressedUp:withEvent:)
forControlEvents:UIControlEventTouchDragOutside];
UIImage *buttonbkImage = [UIImage imageNamed:@"TF8UIElements_smallKeysBtn"];
UIImage *buttonlightImage = [UIImage imageNamed:@"TF8_smallKeysBtnBright"];
[button setBackgroundImage:buttonbkImage forState:UIControlStateNormal];
[button setBackgroundImage:buttonlightImage
forState:UIControlStateHighlighted];
[KeyboardView addSubview:button];
[_buttonArray addObject:button];
}
-(void)clearButtonArray
{
for (int i=0; i < [_buttonArray count]; i++)
{
[[_buttonArray objectAtIndex:i] removeFromSuperview];
[[_buttonArray objectAtIndex:i] setImage:nil forState:UIControlStateNormal];
[[_buttonArray objectAtIndex:i] setImage:nil forState:UIControlStateHighlighted];
}
[_buttonArray removeAllObjects];
}
【问题讨论】:
-
首先确定你的按钮确实是原因。
-
为什么要绑定这么多事件?
-
@Kreiri 是的,我把范围缩小到了这个。禁用整个按钮创建代码,不会增加内存。
标签: ios objective-c uibutton