2017-09-24 7 views
1

私はシンプルなアプリケーションを作成しようとしています。ユーザーがノートを通り、次にクイズを行うことができます。私はこのメモを画像として提示しており、画像は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; 
    } 
} 

すべてがうまく機能しています。しかし、画像が変化するたびに、アプリケーションによって使用されるメモリは数メガバイト増加します。以下は、メモリ使用量の写真です。以降黒線から

Memory Usage Recorded

私は「次へ」ボタンを押したときに、メモリが使用さ上昇。ユーザーがクイズを続けるまで、それは繰り返されます。クイズを終了してノートを始めると、メモリ使用量は以前のメモリ使用量(73MB)から引き続き増加します。最終的に、アプリケーションがクラッシュし、私はもうアプリケーションを起動できません。

私は以前にメモリリークの問題に直面したことがないため、何をすべきか分かりません。私は過去1時間にグーグルで試してみましたが、これに対する解決策を考え出すことができません。だから私の質問は、どのようにメモリを解放したり、メモリの使用量を減らすことができるだろう。

答えて

0

あなたの画像が原因だと思いますが、あなたが見せている画像の大きさはどれくらいですか?私もよく分からない(

-(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の使用率を取り上げる:この回答に基づいて

https://stackoverflow.com/a/22662754/3231194は、あなたのUIImageViewにそれらを置く前に、あなたのイメージを拡大するために、このメソッドを使用しますかそれが大きな問題であれば)。

OR

あなたは自分のXcodeプロジェクトにインポートする前に、自分で画像のサイズを変更することができます。

+1

ありがとうございました。私は手動でイメージのサイズを変更しようとしましたが動作しませんでしたが、イメージを再スケーリングするとメモリ使用量が減少しました。この回答をお寄せいただきありがとうございます。 –

関連する問題