iOS6后的内存警告处理

iOS6后的内存警告处理

  The memory used by a view to draw itself onscreen is potentially quite large. However, the system automatically releases these expensive resources when the view is not attached to a window. The remaining memory used by most views is small enough that it is not worth it for the system to automatically purge and recreate the view hierarchy.

  iOS6后UIKit框架会自动将view中的图片资源给释放掉,而不会将view对象本身给释放掉。因为和位图内存相比,view对象本身所占内存小到可以忽略。所以iOS6起后,didReceiveMemoryWarning默认会做的事情是把把位图资源释放掉,简而言之就是程序员不用再考虑了,UIKit会自动做。而view对象都会被保留。

  如果程序员需要自己释放资源,可以这像下面这样写。

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Add code to clean up any of your own resources that are no longer necessary.
    if (self.isViewLoaded && [self.view window] == nil)
    {
        // Add code to preserve data stored in the views that might be
        // needed later.
 
        // Add code to clean up other strong references to the view in
        // the view hierarchy.
        self.view = nil;
    }

  iOS5默认会释放位图资源与卸载view对象,iOS6默认只释放位图资源。而为了统一处理,可以用上述代码,iOS6后自主卸载view对象。另外viewWillUnload与viewDidUnload这2个方法就没有意义了,把想关代码都移到上述代码中即可。

参考:1、https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/ViewLoadingandUnloading/ViewLoadingandUnloading.html

2、http://justsee.iteye.com/blog/1820588

原文地址:https://www.cnblogs.com/tekkaman/p/3693339.html