2009-08-12 34 views
6

我的应用程序使用大量内存。通常情况下,它运行良好,但是在一段时间内尚未重新启动的加载设备上,它将因臭名昭着的低内存错误而被抛弃。如何在OpenGL应用程序中响应didReceiveMemoryWarning

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

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

我能做些什么来捕获didReceiveMemoryWarning消息以便我可以回复?

回答

9

这也可在您的Application Delegate

-(void)applicationDidReceiveMemoryWarning:(UIApplication *)application 
{ 
    NSLog(@"Received memory warning!"); 
} 
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; 
} 
相关问题