2014-10-29 61 views
0

更新:Xcode處置內存 - 圖像佔用太多內存,不會釋放內存

我有很多視圖控制器和應用程序是圖像沉重。每一個都是現在以模態方式呈現。我正在處理所有的圖像,計時器等等,其他任何視圖都會在每個視圖控制器上出現,但是當它繼續進行時,它似乎是在前面的VC和其他所有內容上不斷分配內存,而沒有擺脫一件事情最終崩潰(我知道這將發生在模態賽段)。 我該如何改變這一點?我試圖將我的控制器嵌入到導航控制器中,但同樣的問題仍然發生在它不會釋放控制器上的任何圖像的地方。以下是我的代碼示例,以及我擺脫在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]; 

} 
+0

你調用的'viewWillDissapear'方法嗎?請發佈代碼,我們可以幫助 – 2014-10-29 11:28:51

回答

0

幾件事。

如果您以模態方式呈現視圖控制器,它們會疊放在下一個視圖控制器之上,並且它們都不會在您解散視圖控制器之前釋放。確保你沒有創建一個以前的視圖控制器的新實例,並以模態方式推動它,而不是退回到所需的VC。

其次,

它曾經是,當一個視圖控制器移到屏幕外,它的視圖層次被卸載,釋放了大部分花的記憶。這不再是事實。 (並沒有出現過不少主要的iOS版本,我想說這個在iOS 4中有所改變,但我不是這麼看好)。

如果一個視圖控制器在內存中,它也是整個視圖層次結構包括在這些視圖內的圖像視圖中安裝的任何圖像。

您確實可以實現vi​​ewWillDisappear方法,並使用該方法來清除未在視圖層次結構中安裝的任何數據結構。然後你需要實現一個重載這些數據結構的viewWillAppear方法。

如果您的應用程序以模態方式呈現一系列視圖控制器,然後用戶「展開」每個視圖控制器並返回到根視圖控制器,那麼使用模態表示的方法是合理的。 (使用導航控制器和一堆推送的視圖控制器可能會更好,但您聲稱沒有。)兩種方法都將以前訪問過的所有視圖控制器堆疊在內存中。

如果用戶的用戶體驗讓用戶從VC移到VC而無需返回,則應改爲實現自己的自定義Segue,該關閉會關閉先前的視圖控制器,或者使用它交換另一個的子視圖控制器隨着用戶導航。

+0

感謝您的回答,這是非常有益的,但我仍然有同樣的問題。該應用程序以一些您不需要返回的標題視圖開始,然後展開每個視圖並且用戶將返回到根視圖。我已經將我的視圖控制器嵌入到導航控制器中,並且看起來更好,但是如您所說,這兩種方法都會將以前訪問過的VC保留在內存中。我想要做的是在視圖控制器已經被查看之後從內存中移除(並且如果需要的話再次重新加載它們),但是它似乎保留在視圖中 – Haz 2014-11-03 13:57:11