2010-08-31 42 views
0

我正在生成一個簡單的scrollView,其中一些圖像附加到按鈕上。 這個工作很好,除了這個滾動視圖佔用了很多內存。當前從多個圖像的UIScrollview釋放內存的方式

因爲這個scrollView只是一個子菜單,允許用戶選擇一個圖像,不久之後我不需要它,我想從內存中釋放這個沉重的塊。

你能好心幫助我理解這個問題,並免費在不需要時

int flipFlop = 1; 
masksAvailable = 18; 
float topMaskXX = 85.0; 
float topMaskYY = 96.0; 
UIButton *button; 
for (int buttonsLoop = 1;buttonsLoop < masksAvailable+1;buttonsLoop++){ 


    button = [UIButton buttonWithType:UIButtonTypeCustom]; 
    NSString *tempname = [NSString stringWithFormat:@"mask_frame%i.png",buttonsLoop]; 

    // This fellow there is the memory eating monster 
    [button setBackgroundImage:[UIImage imageNamed:tempname] forState:UIControlStateNormal]; 

    tempname = nil; 

    button.tag = buttonsLoop; 
    [button addTarget:self action:@selector(handleMaskKeys:) forControlEvents:UIControlEventTouchUpInside]; 


    UIImageView *frameForSubImages = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image_frame.png"]]; 
    frameForSubImages.frame = CGRectMake(0.0, 0.0, 320.0/2.9, 480.0/2.9); 
    frameForSubImages.center = CGPointMake(topMaskXX,topMaskYY); 
    [scrollView addSubview:frameForSubImages]; 


    button.frame = CGRectMake(0.0, 0.0, 320.0/3.4, 480.0/3.4); 
    button.center = CGPointMake(topMaskXX,topMaskYY); 
    if (flipFlop == 1){ 
    topMaskXX += 150; 
    } else { 
    topMaskYY += 185.0; 
    topMaskXX = 85.0; 

    } 
    flipFlop = flipFlop * -1; 
    [scrollView addSubview:button]; 






} 

回答

0

首先,這個巨大的內存塊,我想建議你做一個「乾淨所有」和「建立和分析」你的項目。它非常善於指出保留/釋放的問題。其次,任何保留對象的類都應該定義一個釋放這些對象的「dealloc」,以確保它們在釋放對象時被刪除。

-(void) dealloc { 
    // release all retained objects here. 

    [super dealloc]; 
} 

第三,在你上面的例子,它也像frameForSubImages可能會對它的一個額外保留,因爲你已經分配它(+1參考),並沒有它分配到一個視圖(+1參考)曾經調用釋放(這將是-1引用,並留下1的refcount)。

最後,我還建議您閱讀iOS的Memory Management Programming Guide