4

UIView.layer の renderInContext メソッドを使用して、複数の pdf ファイルを作成しようとしています。

以下のコードは autoreleasepool で実行されます。

---loop

    UIGraphicsBeginPDFContextToFile(documentDirectoryFilename, CGRectZero, nil);

    UIGraphicsBeginPDFPage();

    CGContextRef pdfContext = UIGraphicsGetCurrentContext();

    [view.layer renderInContext:pdfContext];

    view =nil;

    pdfContext =nil
    UIGraphicsEndPDFContext()

--loop ends

数回の反復の後、常駐メモリが 20 mb に増加し、その後の反復により、使用されるメモリの合計が増加します。

ARC が PDF のレンダリングに使用されたメモリを解放していないため、メモリ不足でアプリケーションがクラッシュすることがあります。

アプリケーションは ARC を使用しています

問題を解決するための助けや指針をいただければ幸いです。

ありがとう

更新: 素早い返信ありがとうございます。

疑似コードにタイプミスがあったことをお詫びします。テスト プロジェクトのサンプル コードを次に示します。

   -(BOOL)addUIViewToPDFFile:(UIView*)view newBounds:(CGRect)newBounds pdfFile:(NSString*)pdfFile{
BOOL retFlag =YES;

    NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);

    NSString* documentDirectory = [documentDirectories objectAtIndex:0];
    NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:pdfFile];
    UIGraphicsBeginPDFContextToFile(documentDirectoryFilename, CGRectZero, nil);
   CGContextRef pdfContext = UIGraphicsGetCurrentContext();
    if([NSThread isMainThread]){
        NSLog(@"Main Thread");
    }else{
        NSLog(@"Not in Main Thread");

    }
    UIGraphicsBeginPDFPage();
    logMemUsage1();
    [view.layer renderInContext:pdfContext];
    UIGraphicsEndPDFContext();
    pdfContext = nil;
    logMemUsage1();


return retFlag;

 }

  - (IBAction)createPdf:(id)sender {
    for(int i=0;i<10;i++){

     @autoreleasepool {
    PDFViewController *vc = [[PDFViewController alloc] init];
    [vc loadView];
    NSString *PdfFileName =[ NSString stringWithFormat:@"TestPdf%d.pdf",i ];
    [self addUIViewToPDFFile:vc.view newBounds:self.view.frame pdfFile:PdfFileName];
    vc = nil;

  }

} }

もう 1 つ追加する必要があるのは、アプリケーションがハードウェアのホーム ボタンを押してバックグラウンドになり、再びフォアグラウンドに戻ると、メモリがクリアされて解放されたように見えることです。

各反復の後、pdfの作成に使用されるメモリが解放されることを期待しています。

4

0 に答える 0