2012-02-27 84 views
3

在Apple文档中,您可以发现Apple建议在应用程序进入后台模式时释放大量数据,如图像。
如何从UIViews和其他数据发布图像?
如何从所有viewController正确的方式发布UIViews的图片?
如何在应用程序获取applicationWillResignActive消息时恢复数据?

如果有人有一个很好的例子或链接,请显示它。当应用程序进入后台模式时释放图形

+4

为什么这么多downvotes?好的,这是一个模糊的问题,但实际上是一个很好的问题。实际上很高兴看到这个答案(即对初学者有用)。 – 2012-02-27 15:49:34

回答

1

添加到应用程序委托2种方法

- (void)applicationDidEnterBackground:(UIApplication *)application 
{ 
    [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_APP_BACKGROUND object:nil]; 
} 

- (void)applicationWillEnterForeground:(UIApplication *)application 
{ 
    [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_APP_BACKGROUND object:nil]; 
} 

让BaseViewController与方法:从BaseViewController

- (id)init 
{ 
    self = [super init]; 
    if (self) { 
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillForeground) name:NOTIFICATION_APP_FOREGROUND object:nil]; 
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidBackground) name:NOTIFICATION_APP_BACKGROUND object:nil]; 
    } 

    return self; 
} 


- (void)appDidBackground { 
} 

- (void)appWillForeground { 

} 
- (void)dealloc { 
    [[NSNotificationCenter defaultCenter] removeObserver:self]; 
    [super dealloc]; 
} 

子类所有的视图控制器。在方法appDidBackground你应该释放不需要的数据,在appWillForeground - 恢复它

+0

1.有人告诉我,如果使用'[UIImage imagenamed:@“image.png”]' - 这将不会释放内存中的图像。 2.如何保存你的数据然后恢复。使用NSUserDefaults?如果你使用coreData,这很容易。但是,例如,如何保存nsarray,我可以将它保存到nsuserdefaults或plist文件中。什么是正确的方式? – rowwingman 2012-02-28 12:30:36

+0

1. [UIImage imageNamed]缓存所有图像。它可能会在内存警告中释放。 2.为了节省大量数据,您应该为用户coredata提供一些数据 - NSUserDefaults是一种好方法。如果你想用一些自定义对象保存一个数组(在NSUserDefaults中),你应该为其中的每个自定义类实现NSCoding协议。 – NeverBe 2012-02-28 12:57:40

+0

如何发布使用loadView添加的UIView。有些人说需要使用viewDidUnload,但是app进入后台模式,这个方法不会调用。 – rowwingman 2012-07-19 19:03:00

相关问题