iOS6后的内存警告处理

iOS6后的内存警告处理html

  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.ios

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

  若是程序员须要本身释放资源,能够这像下面这样写。app

- (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个方法就没有意义了,把想关代码都移到上述代码中便可。框架

参考:一、https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/ViewLoadingandUnloading/ViewLoadingandUnloading.htmlui

二、http://justsee.iteye.com/blog/1820588spa

相关文章
相关标签/搜索