私はシンプルなアプリケーションを作成しようとしています。ユーザーがノートを通り、次にクイズを行うことができます。私はこのメモを画像として提示しており、画像はUIImageView
に割り当てられてユーザーに表示されます。ボタンが押されたときのメモリのリーク
私の見解では、ロードがほとんどUIを設定しています。それから私は、画像(ノート)の私の配列を含む
- (void)viewDidLoad {
UIColor *colourForNavigationBar = [self colorWithHexString:@"919D9F"];
[[UINavigationBar appearance] setBarTintColor:colourForNavigationBar];
UIColor *colourForBackground = [self colorWithHexString:@"F0F3F4"];
self.view.backgroundColor = colourForBackground;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:27.0];
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor whiteColor];
self.navigationItem.titleView = label;
label.text = @"Notes";
[label sizeToFit];
UIColor *colourForButton = [self colorWithHexString:@"919D9F"];
self.previousButton.backgroundColor = colourForButton;
self.nextButton.backgroundColor = colourForButton;
finishedNotes = NO;
playerPlaying = NO;
arrayOfImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"1.png"],
[UIImage imageNamed:@"2.png"],
[UIImage imageNamed:@"3.png"],
[UIImage imageNamed:@"4.png"],
[UIImage imageNamed:@"5.png"],
[UIImage imageNamed:@"6.png"],
[UIImage imageNamed:@"7.png"],
[UIImage imageNamed:@"8.png"],
[UIImage imageNamed:@"9.png"],
[UIImage imageNamed:@"10.png"],
[UIImage imageNamed:@"11.png"],
[UIImage imageNamed:@"12.png"],
[UIImage imageNamed:@"13.png"],
nil];
}
を私の配列を作成することでしょうその後、ユーザは次のノートへの継続を可能にする「次へ」ボタンがあります。以下はアクションのコードです
- (IBAction)nextNote:(id)sender {
if (finishedNotes == YES) {
[self performSegueWithIdentifier:@"quizFromNotes" sender:self];
}
self.previousButton.enabled = YES;
stateOfPhoto++;
if (stateOfPhoto < 13) {
UIImage *image = [arrayOfImages objectAtIndex:stateOfPhoto-1];
self.imageView.image = image;
}
if (stateOfPhoto == 13) {
UIImage *image = [arrayOfImages objectAtIndex:stateOfPhoto-1];
self.imageView.image = image;
[self.nextButton setTitle:@"Begin Quiz" forState:UIControlStateNormal];
finishedNotes =YES;
}
if (playerPlaying == YES) {
[self.notesPlayer stop];
playerPlaying = NO;
}
}
すべてがうまく機能しています。しかし、画像が変化するたびに、アプリケーションによって使用されるメモリは数メガバイト増加します。以下は、メモリ使用量の写真です。以降黒線から
私は「次へ」ボタンを押したときに、メモリが使用さ上昇。ユーザーがクイズを続けるまで、それは繰り返されます。クイズを終了してノートを始めると、メモリ使用量は以前のメモリ使用量(73MB)から引き続き増加します。最終的に、アプリケーションがクラッシュし、私はもうアプリケーションを起動できません。
私は以前にメモリリークの問題に直面したことがないため、何をすべきか分かりません。私は過去1時間にグーグルで試してみましたが、これに対する解決策を考え出すことができません。だから私の質問は、どのようにメモリを解放したり、メモリの使用量を減らすことができるだろう。
ありがとうございました。私は手動でイメージのサイズを変更しようとしましたが動作しませんでしたが、イメージを再スケーリングするとメモリ使用量が減少しました。この回答をお寄せいただきありがとうございます。 –