简体   繁体   中英

Effectively release UIButton memory

I have the following function which creates a custom button. Every time I call initKeyboard(), It is called 14 times. In the course of my app, the user presses a button which calls initKeyboard many times. Every time the user presses the button, I call clearButtonArray().

I noticed that the memory in use goes up gradually, and when it reaches 200MB or so I do see some visual slowdown in my app. Animations are not smooth, etc.

My question is, how do I effectively release the memory used by the 14 buttons everytime. It looks like clearButtonArray() is not doing the job.

I am using ARC.

Thank you for your help.

- (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];
}

Remove all the targets of the UIButton before removing it, to avoid keep references.

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

I would recommend to remove them all at once from the KeyboardView and not in a for loop.

So if the KeyboardView only has the buttons as subviews:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM