2017-09-24 33 views
1

我正在尝试做一个简单的应用程序,用户可以通过笔记然后做一个测验。我将这些笔记作为图像呈现,图像将被分配给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; 
    } 
} 

一切工作正常,顺利。但是,每当图像发生变化时,应用程序使用的内存就会增加几兆字节。以下是内存使用情况的照片。

Memory Usage Recorded

从黑线起,当我按下“下一步”按钮,存储器中使用的增加。它只是重复自己,直到用户继续参加测验。如果我结束测验并重新开始笔记,则内存使用量将继续从先前的内存使用量(73 MB)上升。最终,应用程序会崩溃,我无法启动应用程序了。

我从来没有遇到内存泄漏问题,因此我不知道该怎么做。我曾尝试在过去一小时内使用Google搜索,但无法为此提供解决方案。所以我的问题是我将如何释放内存或减少内存使用量。

回答

0

我认为这是因为你的图像,你显示的图像有多大?

基于这样的回答:https://stackoverflow.com/a/22662754/3231194,您可以用这种方法来扩展您的图像把它们放入您的UIImageView前:

-(UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize { 
    //UIGraphicsBeginImageContext(newSize); 
    // In next line, pass 0.0 to use the current device's pixel scaling factor (and thus account for Retina resolution). 
    // Pass 1.0 to force exact pixel size. 
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0); 
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)]; 
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return newImage; 
} 

但是,这会占用CPU使用率(我不知道,虽然如果它是一个大问题)。

OR

您可以自行其导入到您的Xcode项目之前刚刚调整图片的大小。

+1

非常感谢。我试图手动调整图像大小,但不起作用,但重新缩放图像确实有助于减少内存使用量。感谢这个答案,并对迟到的回复感到抱歉。 –