如何在OpenGL应用程序中响应didReceiveMemoryWarning

Fra*_*ger 6 iphone opengl-es low-memory didreceivememorywarning

我的应用程序使用了大量内存.通常它运行正常,但是在一段时间内没有重新启动的加载设备上,它将被抛弃臭名昭着的低内存错误.

我想回应didReceiveMemoryWarning并释放一些我的缓存.

但我有一个问题,我的应用程序是基于OpenGL ES模板,没有视图控制器.它只有App Delegate,它包含对glView的引用.

如何捕获didReceiveMemoryWarning消息以便我可以做出响应?

Tyl*_*ler 10

您还可以将方法作为观察者在所需的任何类中添加到UIApplicationDidReceiveMemoryWarningNotification通知中.代码可能是这样的:

- (void) cleanMemory: (NSNotification*) notification {
  // Save memory!
}

- (id) init {  // Or any other function called early on.
  // other init code
  [[NSNotificationCenter defaultCenter]
   addObserver:self selector:@selector(cleanMemory:)
          name:UIApplicationDidReceiveMemoryWarningNotification
        object:nil];
  return self;
}
Run Code Online (Sandbox Code Playgroud)


NWC*_*der 9

您的应用程序代表中也可以使用此功能.

-(void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
  NSLog(@"Received memory warning!");
}
Run Code Online (Sandbox Code Playgroud)