【问题标题】:Memory leakage when button is pressed按下按钮时内存泄漏
【发布时间】:2018-03-05 09:01:50
【问题描述】:

我正在尝试制作一个简单的应用程序,用户可以在其中浏览笔记然后进行测验。我将此笔记作为图像呈现,并且图像将分配给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;
    }
}

一切都运行良好且顺利。但是,每当图像发生变化时,应用程序使用的内存就会增加几兆字节。下面是内存使用情况的照片。

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

我以前从未遇到过内存泄漏问题,因此我不知道该怎么办。在过去的一个小时里,我尝试使用谷歌搜索,但无法找到解决方案。所以我的问题是如何释放内存或减少内存使用量。

【问题讨论】:

    标签: ios objective-c xcode memory


    【解决方案1】:

    我想是因为你的图片,你展示的图片有多大?

    基于此答案: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 的使用率(我不确定这是否很重要)。

    您可以在将图像导入 Xcode 项目之前自行调整图像大小。

    【讨论】:

    • 非常感谢。我尝试手动调整图像大小,但没有用,但重新调整图像确实有助于减少内存使用量。感谢您的回答,很抱歉回复晚了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-01
    • 2021-08-18
    • 2015-06-16
    • 2015-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多