2010-08-31 51 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