我正在尝试做一个简单的应用程序,用户可以通过笔记然后做一个测验。我将这些笔记作为图像呈现,图像将被分配给UIImageView
以向用户显示。按下按钮时内存泄漏
在我看来确实加载我主要是设置用户界面。然后,我会创建一个包含我的图片(注)阵列我的数组
- (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;
}
}
一切工作正常,顺利。但是,每当图像发生变化时,应用程序使用的内存就会增加几兆字节。以下是内存使用情况的照片。
从黑线起,当我按下“下一步”按钮,存储器中使用的增加。它只是重复自己,直到用户继续参加测验。如果我结束测验并重新开始笔记,则内存使用量将继续从先前的内存使用量(73 MB)上升。最终,应用程序会崩溃,我无法启动应用程序了。
我从来没有遇到内存泄漏问题,因此我不知道该怎么做。我曾尝试在过去一小时内使用Google搜索,但无法为此提供解决方案。所以我的问题是我将如何释放内存或减少内存使用量。
非常感谢。我试图手动调整图像大小,但不起作用,但重新缩放图像确实有助于减少内存使用量。感谢这个答案,并对迟到的回复感到抱歉。 –