代码之家  ›  专栏  ›  技术社区  ›  Chandrasekaran Dinesh

按下按钮时内存泄漏

  •  1
  • Chandrasekaran Dinesh  · 技术社区  · 9 年前

    我正在尝试制作一个简单的应用程序,用户可以在其中浏览笔记,然后做一个测试。我将这些笔记作为图像呈现,图像将分配给 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)上升。最终,应用程序会崩溃,我无法再启动应用程序了。

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

    1 回复  |  直到 9 年前
        1
  •  0
  •   Glenn Posadas    9 年前

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

    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使用量(我不确定这是否是一个大问题)。

    或