2017-09-24 71 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

非常感謝。我試圖手動調整圖像大小,但不起作用,但重新縮放圖像確實有助於減少內存使用量。感謝這個答案,並對遲到的回覆感到抱歉。 –