2012-10-18 75 views
0

在我看來,我有近45 45 UIButton s,我知道按鈕沒有被分配到內存,不知何故編譯器分配/釋放它,但我注意到在這個視圖,設備變得更慢,所以...什麼我應該如何避免記憶韭菜與UIButtonUIButton內存管理 - 如何管理它?

這是我如何把我的按鈕,進入我的觀點:

在myView.h

UIButton *btn1; 
在myView.m

btn1 = [UIButton buttonWithType:UIButtonTypeCustom]; 
[btn1 setTitle:@"btn1" forState:UIControlStateNormal]; 
[btn1 addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside]; 
btn1.backgroundColor = [UIColor clearColor]; 
btn1.frame = CGRectMake( arc4random() % 920, arc4random() %600+50 , 65, 65); 
[self.view addSubview:btn1]; 

,我怎麼刪除它:

for(UIButton* b in [self.view subviews]){ 
    [b removeFromSuperview]; 
    b = nil; 

} 
+0

請顯示一些代碼:你如何將按鈕添加到視圖?你如何創建它們? – sergio

+0

我在.h文件中定義它們 – Mutawe

+0

bt你如何爲它們分配內存?你如何釋放它?郵政編碼! – mayuur

回答

4

他們的內存泄漏你的應用程序不會產生顯着的減速。它們的影響正在填滿內存,並且如果數量足夠多,操作系統會由於內存過度使用而殺死您的應用程序。 (如果操作系統沒有殺死你的應用程序,那麼你可能會放慢速度,但因爲它殺死了應用程序,所以沒有問題)。

在任何情況下,如果您擔心內存泄漏,可以啓動儀器並查看顯示視圖時是否檢測到任何內存泄漏。

可能你觀察速度慢的原因是必須加載到內存中45張圖像,從磁盤讀取(這很慢)。你可能會想到預加載它們,並看看這是否改善了事情。

一個簡單的方法來預加載的圖像是通過imageNamed實例吧:

UIImage* img = [UIImage imageNamed:@"myButton1.png"]; 

你可以調用該方法對於所有的按鈕和收集,你可以調用其他方法,例如,所有相關電話,在啓動時,或在任何其他適合您的時刻。

編輯:

當我進入視圖和退出並重新進入視圖等

只是注意:您不需要從上海華刪除按鈕(在正常條件):這將在移除超級視圖時由框架完成。可能是這會減慢應用程序的速度嗎?另外,如果退出/進入視圖時發生問題,您是否可以嘗試「緩存」整個視圖(如:僅實例化一次,然後在需要時顯示它)。

+0

不,我的按鈕沒有圖像。 – Mutawe

+0

我明白了......呃,這只是一個猜測,然後才顯示代碼......順便說一下,最初顯示視圖或準確顯示時會發生減速? – sergio

+0

當我進入視圖並退出並重新進入視圖等 – Mutawe

0

您可以使用

@autoreleasepool { //You UIButtons creation code goes here } 

,這將釋放內存,autoreleasepool範圍內創建。我使用autoreleasepool爲我的cellForAtIndexPath方法,其中有時我有超過10000行與自定義單元格和一些數據庫查詢。

+0

你能舉個例子嗎? – Mutawe

+0

檢查我的其他答案。 –

0

我以這種方式使用autorelease池。這將釋放autorelease內更多的創建。 我很多autoreleasepool添加到我的表以這種方式獲得更好的性能

@autoreleasepool { 
        UIButton *btn=(UIButton*)[cell3.contentView.subviews objectAtIndex:btncont]; 

        btn.tag=i; 
        [btn addTarget:self action:@selector(MozaicTblBtnSelection:) forControlEvents:UIControlEventTouchUpInside]; 

        UIImage* img = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@/downloads/%@/files/%@.jpg",del.LocalPath,del.CompFolder,del.RepId,pi.STOCK_CODE]]; 

        if(img!=nil) 
        { 
         [btn setImage:img forState:UIControlStateNormal]; 

         [btn.layer setBorderColor:[UIColor blackColor].CGColor]; 
         [btn.layer setBorderWidth:1.0]; 

        } 
        else 
        { 
         [btn setImage:[UIImage imageNamed:@"no_privew95x77.jpg"] forState:UIControlStateNormal]; 

        } 
       } 
+0

對我不明確 – Mutawe

0

我希望這可以幫助你......

UIScrollView *scrlView =[[UIScrollView alloc]initWithFrame:self.view.frame]; 
    scrlView.tag=123; 
    [scrlView setBackgroundColor:[UIColor blueColor]]; 

    for(int i=0;i<45;i++) 
    { 
     UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom]; 
     [btn1 setTitle:@"OK" forState:UIControlStateNormal]; 
     btn1.tag=i; 
     [btn1 addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside]; 
     btn1.backgroundColor = [UIColor clearColor]; 
     btn1.frame = CGRectMake( arc4random() % 300, arc4random() %420 , 30, 30); 
     [scrlView addSubview:btn1]; 
    } 
    [scrlView setContentSize:CGSizeMake(320, 460)]; 
    [self.view addSubview:scrlView]; 

&得到CickEvent

-(IBAction)btnClicked:(UIButton*)sender 
    { 
     NSLog(@"Button No :%d Clicked",sender.tag); 
    } 

&爲刪除你可以做的是

for(UIButton* b in [[self.view viewWithTag:123] subviews]) 
    { 
     [b removeFromSuperview]; 
    } 
+0

我已經嘗試添加5000個按鈕,它仍然不到一秒鐘,如果問題仍然存在,您應該檢查代碼的其他部分 – Anand