Xcode处理内存 - 占用过多内存但不会释放内存的图像

时间:2014-10-29 11:09:39

标签: ios objective-c xcode memory modal-dialog

更新:

我有很多视图控制器,应用程序图像很重。每个都是现在以模态呈现。我正在处理我的所有图像,定时器等以及在每个视图控制器上出现的任何其他视图,但是当它被分割时,它似乎只是在前一个VC和其他所有内容之间不断地分配内存而没有删除任何一个最终崩溃了(我知道这将发生模态segues)。 我怎么能改变这个?我已经尝试将我的控制器嵌入到导航控制器中,但是仍然会发生同样的问题,它不会释放控制器上的任何图像。这是我的代码的一个例子,我摆脱了我在VWD中创建的内容。 VWD肯定被称为。希望这对某人有意义,提前感谢。

@interface AnimationStartViewController ()

@end

@implementation AnimationStartViewController




SystemSoundID skipintro;



-(void) viewDidLoad
{


 [super viewDidLoad];



(sleep(2.5));
NSString *music=[[NSBundle mainBundle]pathForResource:@"1Animation" ofType:@"mp3"];
animationaudio=[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:music] error:NULL];
animationaudio.delegate= (id)self;
animationaudio.numberOfLoops=-1;
[animationaudio play];


NSURL *skipintroURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Click" ofType:@"mp3"]];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)skipintroURL, &skipintro);


//Text Timers

text1timer = [NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(Text1show:) userInfo:nil repeats:NO];
text2timer = [NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(Text2show:) userInfo:nil repeats:NO];
text3timer =  [NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(Text3show:) userInfo:nil repeats:NO];
text4timer =  [NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(Text4show:) userInfo:nil repeats:NO];

text5timer = [NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(Text5show:) userInfo:nil repeats:NO];
text6timer = [NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(Text6show:) userInfo:nil repeats:NO];
text7timer = [NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(Text7show:) userInfo:nil repeats:NO];
text8timer = [NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(Text8show:) userInfo:nil repeats:NO];


logotimer = [NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(showlogo) userInfo:nil repeats:NO];
pushtimer = [NSTimer scheduledTimerWithTimeInterval:20.0 target:self selector:@selector(performthesegue) userInfo:nil repeats:NO];



//Text Timers invalidate


stoptext1timer = [NSTimer scheduledTimerWithTimeInterval:6.0 target:self selector:@selector(invalidatetext1) userInfo:nil repeats:NO];
stoptext2timer = [NSTimer scheduledTimerWithTimeInterval:6.0 target:self selector:@selector(invalidatetext2) userInfo:nil repeats:NO];
stoptext3timer = [NSTimer scheduledTimerWithTimeInterval:6.0 target:self selector:@selector(invalidatetext3) userInfo:nil repeats:NO];
stoptext4timer = [NSTimer scheduledTimerWithTimeInterval:6.0 target:self selector:@selector(invalidatetext4) userInfo:nil repeats:NO];

stoptext5timer = [NSTimer scheduledTimerWithTimeInterval:14.5 target:self selector:@selector(invalidatetext5) userInfo:nil repeats:NO];
stoptext6timer = [NSTimer scheduledTimerWithTimeInterval:14.5 target:self selector:@selector(invalidatetext6) userInfo:nil repeats:NO];
stoptext7timer = [NSTimer scheduledTimerWithTimeInterval:14.5 target:self selector:@selector(invalidatetext7) userInfo:nil repeats:NO];
stoptext8timer = [NSTimer scheduledTimerWithTimeInterval:14.5 target:self selector:@selector(invalidatetext8) userInfo:nil repeats:NO];


moveAnimationTimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(moveAnimation) userInfo:nil repeats:YES];

stopAnimationTimer = [NSTimer scheduledTimerWithTimeInterval:20.0 target:self selector:@selector(StopAnimation) userInfo:nil repeats:NO];



}



//scrolling Animation.

- (void) moveAnimation {
animation.center = CGPointMake(animation.center.x, animation.center.y -2);

}


//Stop

- (void) StopAnimation {
[moveAnimationTimer invalidate];
moveAnimationTimer = nil;


}




//====================== Text animations





//AttheGateof

-(void)Text1show:(NSTimer *)timer {


[UIView animateWithDuration:2.0 delay:0.70
                    options:UIViewAnimationOptionTransitionCrossDissolve
                 animations:^ {
                     Atthegateof.alpha = 1.0f;
                 }
                 completion:^(BOOL finished) {
                     Atthegateof.alpha = 1.0f;
                 }];
}



//MidnightZoo

-(void)Text2show:(NSTimer *)timer {



[UIView animateWithDuration:2.0 delay:1.75
                    options:UIViewAnimationOptionCurveEaseInOut
                 animations:^ {
                     MidnightZoo.alpha = 1.0f;
                 }

                 completion:^(BOOL finished) {
                     MidnightZoo.alpha = 1.0f;
                 }];
}



//Who lives inside

-(void)Text3show:(NSTimer *)timer {

[UIView animateWithDuration:2.0 delay:2.9
                    options:UIViewAnimationOptionCurveEaseInOut
                 animations:^ {
                     Wholivesinside.alpha = 1.0f;
                 }
                 completion:^(BOOL finished) {

                 }];
}


// Thedeepblackblue

-(void)Text4show:(NSTimer *)timer {

[UIView animateWithDuration:2.0 delay:3.3
                    options:UIViewAnimationOptionCurveEaseInOut
                 animations:^ {
                     Thedeepblackblue.alpha = 1.0f;
                 }
                 completion:^(BOOL finished) {

                 }];
}



//unlockthegate

-(void)Text5show:(NSTimer *)timer {

[UIView animateWithDuration:2 delay:7
                    options:UIViewAnimationOptionCurveEaseInOut
                 animations:^ {
                     Unlockthegate.alpha = 1.0f;
                 }
                 completion:^(BOOL finished) {

                 }];
}
 //tocomeinside

-(void)Text6show:(NSTimer *)timer {
//music 'high' point
[UIView animateWithDuration:2 delay:7.5
                    options:UIViewAnimationOptionCurveEaseInOut
                 animations:^ {
                     Tocomeinside.alpha = 1.0f;
                 }
                 completion:^(BOOL finished) {

                 }];
}
//behindeachlock

-(void)Text7show:(NSTimer *)timer {

[UIView animateWithDuration:2 delay:8.2
                    options:UIViewAnimationOptionCurveEaseInOut
                 animations:^ {
                     Behindeachlock.alpha = 1.0f;
                 }
                 completion:^(BOOL finished) {

                 }];
}
//something hides

-(void)Text8show:(NSTimer *)timer {

[UIView animateWithDuration:2 delay:8.7
                    options:UIViewAnimationOptionCurveEaseInOut
                 animations:^ {
                     Somethinghides.alpha = 1.0f;
                 }
                 completion:^(BOOL finished) {


                 }];
}




 //======================================= Get rid of text




//indvalidate 1
-(void)invalidatetext1 {
[stoptext1timer invalidate];
stoptext1timer = nil;

[UIView animateWithDuration:0.7 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
    Atthegateof.alpha = 0;

} completion:^(BOOL finished) {

    [Atthegateof removeFromSuperview];

}  ];


}

//invalidate 2
-(void)invalidatetext2 {
[stoptext2timer invalidate];
stoptext2timer = nil;
[UIView animateWithDuration:0.7 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
    MidnightZoo.alpha = 0;

} completion:^(BOOL finished) {

    [MidnightZoo removeFromSuperview];

}  ];



}

//invalidate 3
-(void)invalidatetext3 {
[stoptext3timer invalidate];
stoptext3timer = nil;

[UIView animateWithDuration:0.7 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
    Wholivesinside.alpha = 0;

} completion:^(BOOL finished) {

    [Wholivesinside removeFromSuperview];


}  ];



}

//invalidate 4
-(void)invalidatetext4 {
[stoptext4timer invalidate];
stoptext4timer = nil;

[UIView animateWithDuration:0.7 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
    Thedeepblackblue.alpha = 0;

} completion:^(BOOL finished) {

    [Thedeepblackblue removeFromSuperview];


}  ];


}


//invalidate 5
-(void)invalidatetext5 {
[stoptext5timer invalidate];
stoptext5timer = nil;

[UIView animateWithDuration:0.75 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
    Unlockthegate.alpha = 0;

} completion:^(BOOL finished) {

    [Unlockthegate removeFromSuperview];

}  ];

}
//invalidate 6
-(void)invalidatetext6 {
[stoptext6timer invalidate];
stoptext6timer = nil;

[UIView animateWithDuration:0.75 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
    Tocomeinside.alpha = 0;

} completion:^(BOOL finished) {

    [Tocomeinside removeFromSuperview];


}  ];




}
//invalidate 7
-(void)invalidatetext7 {
[stoptext7timer invalidate];
stoptext7timer = nil;

[UIView animateWithDuration:0.75 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
    Behindeachlock.alpha = 0;

} completion:^(BOOL finished) {

    [Behindeachlock removeFromSuperview];

}  ];



}
//invalidate 8
-(void)invalidatetext8 {
[stoptext8timer invalidate];
stoptext8timer = nil;

[UIView animateWithDuration:0.75 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
    Somethinghides.alpha = 0;

} completion:^(BOOL finished) {

    [Somethinghides removeFromSuperview];

}  ];

}




//=======================================



-(void)showlogo {

[UIView animateWithDuration:2.0 delay:16.0
                    options:UIViewAnimationOptionCurveEaseInOut
                 animations:^ {

                     zoowhologo.alpha = 1.0f;
                 }
                 completion:^(BOOL finished) {
                     zoowhologo.alpha = 1.0f;
                 }];

}





-(void)performthesegue {

[self performSegueWithIdentifier:@"pushgototitle" sender:nil];


}


- (void)didReceiveMemoryWarning
{


}



- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];


NSLog(@"viewilldisappear was called");


[animationaudio stop];

[text1timer invalidate], text1timer=nil;
[text2timer invalidate], text2timer=nil;
[text3timer invalidate], text3timer=nil;
[text4timer invalidate], text4timer=nil;
[text5timer invalidate], text5timer=nil;
[text6timer invalidate], text6timer=nil;
[text7timer invalidate], text7timer=nil;
[text8timer invalidate], text8timer=nil;

[stoptext1timer invalidate], stoptext1timer=nil;
[stoptext2timer invalidate], stoptext2timer=nil;
[stoptext3timer invalidate], stoptext3timer=nil;
[stoptext4timer invalidate], stoptext4timer=nil;
[stoptext5timer invalidate], stoptext5timer=nil;
[stoptext6timer invalidate], stoptext6timer=nil;
[stoptext7timer invalidate], stoptext7timer=nil;
[stoptext8timer invalidate], stoptext8timer=nil;

[logotimer invalidate], logotimer=nil;
[pushtimer invalidate], pushtimer=nil;
[moveAnimationTimer invalidate], moveAnimationTimer=nil,
[stopAnimationTimer invalidate], stopAnimationTimer=nil;

AudioServicesDisposeSystemSoundID(skipintro);

[Atthegateof removeFromSuperview], Atthegateof=nil;
[MidnightZoo removeFromSuperview], MidnightZoo=nil;
[Wholivesinside removeFromSuperview], Wholivesinside=nil;
[Thedeepblackblue removeFromSuperview], Thedeepblackblue=nil;
[Unlockthegate removeFromSuperview], Unlockthegate=nil;
[Tocomeinside removeFromSuperview], Tocomeinside=nil;
[Behindeachlock removeFromSuperview], Behindeachlock=nil;
[Somethinghides removeFromSuperview], Somethinghides=nil;
[topper removeFromSuperview], topper=nil;
[skip removeFromSuperview], skip=nil;

[zoowhologo removeFromSuperview], zoowhologo=nil;
[animation removeFromSuperview], animation = nil;
[stopanimation removeFromSuperview], stopanimation = nil;

}


- (void)viewDidDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];


}


- (IBAction)skipintro:(id)sender {

AudioServicesPlaySystemSound(skipintro);
[animationaudio stop];
[pushtimer invalidate];
[self performSegueWithIdentifier:@"pushgototitle2" sender:self];


[self.presentingViewController dismissViewControllerAnimated:NO completion:nil];

}

1 个答案:

答案 0 :(得分:0)

有几件事。

如果您以模态方式呈现视图控制器,它们会在下一个上方叠加,并且在您解除它们之前不会释放它们。确保您没有创建以前的视图控制器的新实例并以模态方式推送它而不是将其解除回所需的VC。

其次,

过去,当视图控制器移出屏幕时,它的视图层次结构被卸载,释放了大部分内存。那不再是真的。 (并且还没有针对相当多的主要iOS版本。我想说这在iOS 4中已经改变了,但我并不乐观。)

如果视图控制器在内存中,那么它的整个视图层次结构也是如此,包括安装在这些视图中的图像视图中的任何图像。

您确实可以实现vi​​ewWillDisappear方法并使用该方法来清除视图层次结构中未安装的任何数据结构。然后,您需要实现一个重新加载这些数据结构的viewWillAppear方法。

如果你的应用程序以模态方式呈现一系列视图控制器,然后用户“展开”每个视图控制器并返回到根视图控制器,那么使用模态显示的方法是合理的。 (使用导航控制器和一堆推送的视图控制器可能会更好,但你声称不是。)这两种方法都会使所有以前访问过的视图控制器的堆栈保持活动状态。

如果你的用户体验让你的用户从VC转移到VC而不需​​要返回,你应该改为实现你自己的自定义segue来解除之前的视图控制器,或者可能使用一个子视图控制器,它交换为另一个视图控制器用户导航。