【问题标题】:The current way to release memory from a UIScrollview with many images当前从具有许多图像的 UIScrollview 释放内存的方法
【发布时间】:2010-08-31 12:41:57
【问题描述】:

我正在生成一个简单的滚动视图,其中一些图像附加到按钮上。 除了这个滚动视图占用大量内存之外,这很好用。

由于此滚动视图只是一个允许用户选择图像的子菜单,并且在我不需要它之后不久,我想从内存中释放这个沉重的块。

您能否帮我理解这个问题,并在不需要时释放这个巨大的内存块

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






}

【问题讨论】:

    标签: ios objective-c memory-management uiscrollview


    【解决方案1】:

    首先,我建议您对您的项目进行“全部清理”和“构建和分析”。它非常擅长指出保留/释放的问题。

    其次,任何保留对象的类都应该定义一个“dealloc”来释放这些对象,以确保它们在对象被释放时被删除。

    -(void) dealloc {
        // release all retained objects here.
    
        [super dealloc];
    }
    

    第三,在你上面的例子中,看起来frameForSubImages 可能有一个额外的保留,因为你已经分配它(+1 引用)并将它分配给一个视图(+1 引用)而没有调用释放(这将是 -1 引用,并为您留下 1 的引用计数)。

    最后,我还建议阅读适用于 iOS 的 Memory Management Programming Guide

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-22
      • 1970-01-01
      • 1970-01-01
      • 2016-05-18
      • 1970-01-01
      • 1970-01-01
      • 2017-12-22
      • 2015-06-21
      相关资源
      最近更新 更多