UIView的animation和内存释放

应用中有这样一个操作,向主视图中分配一个subview,然后添加subview,再使用动画方式退出subview,最后remove掉。
这个过程内存使用量应该时增加,减少这样一个过程,但是在调试时,发现应用内存使用逐步增加,从不释放。

它的实现代码如下所示:


// 日历视图弹出

- (void)slideInCalView:(UITextField *)textField

{

    if (_calView==nil) {

       _calView = [[XYCalendarView allocinitWithFrame:self.view.bounds andSelectedDate:[NSDate datebottomBar:YES];

//        _calView = [[XYCalendarView alloc] initComplexGridWithFrame:self.view.bounds andSelectedDate:[NSDate date] bottomBar:YES];

        

        _calView.delegate = self;

    }

    

    _calView.frame=self.view.bounds;

    

    [self.view addSubview:_calView];

    

    [UIView animateWithDuration:0.4 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{

        _calView.frame = CGRectMake(00_calView.frame.size.width_calView.frame.size.height);

    } completion:^(BOOL finished) {

        

    }];


}

 日历视图关闭 使用delegate method

- (void)slideOutCalendarView:(XYCalendarDay *)selectedTileDay

{

    [UIView animateWithDuration:.5 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{

        _calView.frame = CGRectMake(0_calView.frame.size.height_calView.frame.size.width_calView.frame.size.height);

    } completion:^(BOOL finished) {

        [_calView removeFromSuperview];


    }];

    

    NSDateFormatter *dateFormatter = [[NSDateFormatter allocinit];

    [dateFormatter setDateFormat:@"yyyyMMdd"];

    NSString *strDate = [dateFormatter stringFromDate:selectedTileDay.calDate];

    

    //    GTMLoggerDebug(@"selectedTileDate is %@", strDate);

    

    _editingTextField.text=strDate;

    

    _calView=nil;

    

}



这个错误很隐蔽。
在视图划出屏幕下方后,界面已经看不到。正因为采用了划出动画操作,所以,将该视图设置为nil时,动画还没结束,导致动画结束时的removeFromSuperview发送给来一个nil对象。
所以该子视图还在主视图上,内存还占用。
将nil操作放到动画完成后执行,则内存恢复正常。


    [UIView animateWithDuration:.5 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{

        _calView.frame = CGRectMake(0_calView.frame.size.height_calView.frame.size.width_calView.frame.size.height);

    } completion:^(BOOL finished) {

        [_calView removeFromSuperview];

        NSDateFormatter *dateFormatter = [[NSDateFormatter allocinit];

        [dateFormatter setDateFormat:@"yyyyMMdd"];

        NSString *strDate = [dateFormatter stringFromDate:selectedTileDay.calDate];

        

        //    GTMLoggerDebug(@"selectedTileDate is %@", strDate);

        

        _editingTextField.text=strDate;

        

        _calView=nil;

    }];



奇怪的一点是,释放内存的代码是removeFromSuperview 而不是nil.


不对,还是nil释放内存,只是没有remove掉,表示还有对象在使用它,所以nil时不释放内存。


这里是没有remove掉,那么nil也不会成功。


subview设置为nil后,却没有removefromsuperview,那么占有的内存还在。这就导致来内存泄漏。

非常隐蔽的内存泄漏。


正确的方式是:


先remove后nil才是正确的是否内存的方式。如果还向对象继续留在内存里,下次调用不用定义,这在 主视图退出时nil。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值