didReceiveMemoryWarning

本文详细介绍了iOS应用在不同iOS版本中如何处理内存警告。在iOS 5及更早版本中,系统自动卸载ViewController的视图以节省内存,并调用相应的方法。而在iOS 6及以后版本中,开发者需要手动管理内存,包括释放不再使用的资源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

出处:http://www.jb51.net/article/73025.htm


On iOS 5 and Earlier
1 系统发出警告或者ViewController本身调用导致didReceiveMemoryWarning被调用
2 调用viewWillUnload之后释放View
3 调用viewDidUnload
ios5.0 LeaksDemo 

复制代码代码如下:

 -(void)didReceiveMemoryWarning
{
    //In earlier versions of iOS, the system automatically attempts to unload a view controller's views when memory is low
    [super didReceiveMemoryWarning];

         //didReceiveMemoryWarining 会判断当前ViewController的view是否显示在window上,如果没有显示在window上,则didReceiveMemoryWarining 会自动将viewcontroller 的view以及其所有子view全部销毁,然后调用viewcontroller的viewdidunload方法。


}
- (void)viewDidUnload
{
    // 被release的对象必须是在 viewDidLoad中能重新创建的对象
    // For example: 
       self.myOutlet = nil;
    self.tableView = nil;
    dataArray = nil;
   
    [super viewDidUnload];
}



On iOS 6 and Later

1 系统发出警告或者ViewController本身调用导致didReceiveMemoryWarning被调用
2 - (void)didReceiveMemoryWarning;中释放当前不在使用的资源
ios6.0 LeaksDemo

复制代码代码如下:

 -(void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];//即使没有显示在window上,也不会自动的将self.view释放。注意跟ios6.0之前的区分
    // Add code to clean up any of your own resources that are no longer necessary.
    // 此处做兼容处理需要加上ios6.0的宏开关,保证是在6.0下使用的,6.0以前屏蔽以下代码,否则会在下面使用self.view时自动加载viewDidUnLoad
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 6.0) {
        //需要注意的是self.isViewLoaded是必不可少的,其他方式访问视图会导致它加载,在WWDC视频也忽视这一点。
        if (self.isViewLoaded && !self.view.window)// 是否是正在使用的视图
        {
            // 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;// 目的是再次进入时能够重新加载调用viewDidLoad函数。
        }
    }
}

内存不足时的处理
当我们打开很多应用程序,以及3d游戏时,程序内存不足,会发生内存警告,那内存接下来会左些什么呢????? 
内存警告关乎到一个进程问题!每一个程序都是一个进程,有的进程在程序进入后台之后就开始了休眠,而有些程序进入后台却还一直在运行程序,比如QQ!当控制器接受到内存警告之后,会做如下方法:

1.当控制器接收到内存警告时,会调用 didReceiveMemoryWarning 方法 
2.didReceiveMemoryWarning方法内部的默认实现以下步骤: 首先会检测控制器的view在不在屏幕上

复制代码代码如下:

if (self.view.superview == nil) { // 检测控制器的view在不在屏幕上
// 就会尝试销毁控制器的view
// 即将销毁的时候,就会调用控制器的 viewWillUnload
// 销毁完毕的时候,就会调用控制器的 viewDidUnload方法
} else {

// 不销毁控制器的view


3.当需要再次使用控制器的view时,又会调用loadView方法来创建view

4.接着会调用一系列的生命周期方法 
viewDidLoad —> ……

5.生命周期循环 
loadView –> viewDidLoad –> ..可见.. –内存警告–> didReceiveMemoryWarning —> viewWillUnload –> viewDidUnload —再次使用—> loadView

所以当我们的程序内存过大时,我们挂载在后台的QQ有时候会出现已经推出的情况!当我们再次点击的时候,QQ又重新加载运行起来!

#pragma mark -- UIImagePickerControllerDelegate- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{ UIImage *pickImage = info[UIImagePickerControllerOriginalImage]; CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:@{CIDetectorAccuracy: CIDetectorAccuracyHigh}]; // 获取选择图片中识别结果 NSArray *features = [detector featuresInImage:[CIImage imageWithData:UIImagePNGRepresentation(pickImage)]]; [picker dismissViewControllerAnimated:YES completion:^{ if (features.count > 0) { CIQRCodeFeature *feature = features[0]; NSString *stringValue = feature.messageString; [self handleScanValue:stringValue]; } else { [self readFromAlbumFailed]; } }];}- (void)changeVideoScale:(AVMetadataMachineReadableCodeObject *)objc { NSArray *array = objc.corners; CGPoint point = CGPointZero; int index = 0; CFDictionaryRef dict = (__bridge CFDictionaryRef)(array[index++]); // 把点转换为不可变字典 // 把字典转换为点,存在point里,成功返回true 其他false CGPointMakeWithDictionaryRepresentation(dict, &point); NSLog(@"X:%f -- Y:%f",point.x,point.y); CGPoint point2 = CGPointZero; CGPointMakeWithDictionaryRepresentation((__bridge CFDictionaryRef)array[2], &point2); NSLog(@"X:%f -- Y:%f",point2.x,point2.y); NSLog(@"bounds:%@",NSStringFromCGRect(objc.bounds));}#pragma mark -- App 从后台进入前台- (void)appDidBecomeActive:(NSNotification *)notify { [self resumeScanning];}#pragma mark -- App 从前台进入后台- (void)appWillResignActive:(NSNotification *)notify { [self pauseScanning];}/** 恢复扫一扫功能 */- (void)resumeScanning { if (self.session) { [self.session startRunning]; [self.scannerView startLineAnimation]; }}/** 暂停扫一扫 */- (void)pauseScanning { if (self.session) { [self.session stopRunning]; [self.scannerView stopLineAnimation]; }}#pragma mark -- 扫一扫API/** 处理扫一扫结果 @param value 扫描结果 */- (void)handleScanValue:(NSString *)value { NSLog(@"handleScanValue === %@", value);}/** 相册选取图片无法读取数据 */- (void)readFromAlbumFailed { NSLog(@"readFromAlbumFailed");}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819
04-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值