【问题标题】:Effectively release UIButton memory有效释放 UIButton 内存
【发布时间】: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


【解决方案1】:

在删除之前删除UIButton 的所有目标,以避免保留引用。

[[_buttonArray objectAtIndex:i] removeTarget:nil 
                   action:NULL 
         forControlEvents:UIControlEventAllEvents];

我建议一次性从 KeyboardView 中删除它们,而不是在 for 循环中。

所以如果KeyboardView 只有按钮作为子视图:

[[KeyboardView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
[_buttonArray removeAllObjects];

【讨论】:

  • 没有运气。我仍然看到内存在增加。也许我应该找到一个替代方法来一直创建和销毁按钮?
【解决方案2】:

不是一个真正的答案,但最后我在初始化时只创建了一次取消按钮,并根据我的需要更改了按钮的属性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-04
    • 1970-01-01
    • 2016-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多