1

この質問が何度か聞かれたことは知っていますが、私の特定のケースでは解決できませんでした。CGContextDrawPDFPage は、リーク計測器でリークとして示されます。また、このコード セグメントを実行するとアプリがクラッシュしますが、これはメモリの問題が原因であると確信しています。

    pdfURLDocument = [[NSURL alloc] initFileURLWithPath: [docsDir stringByAppendingPathComponent:documentName]];
    pdfDocument = CGPDFDocumentCreateWithURL((CFURLRef)pdfURLDocument);
    [pdfURLDocument release];

    page = CGPDFDocumentGetPage(pdfDocument, 1);
    CGPDFPageRetain(page);

    // determine the size of the PDF page
    CGRect pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
    pdfScaleWidth = (1/((CGFloat) gridSizeDocument)) * self.frame.size.width/pageRect.size.width;
    pdfScaleHeight = (1/((CGFloat) gridSizeDocument)) * self.frame.size.height/pageRect.size.height;
    pageRect.size = CGSizeMake(pageRect.size.width*pdfScaleWidth, pageRect.size.height*pdfScaleHeight);


    // Create a low res image representation of the PDF page        
    UIGraphicsBeginImageContext(pageRect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    // First fill the background with white.
    CGContextSetRGBFillColor(context, 1.0,1.0,1.0,1.0);
    CGContextFillRect(context,pageRect);

    CGContextSaveGState(context);
    // Flip the context so that the PDF page is rendered
    // right side up.
    CGContextTranslateCTM(context, 0.0, pageRect.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);

    // Scale the context so that the PDF page is rendered 
    // at the correct size for the zoom level.
    CGContextScaleCTM(context, pdfScaleWidth, pdfScaleHeight);
    CGContextSetInterpolationQuality(context, kCGInterpolationHigh); 
    CGContextSetRenderingIntent(context, kCGRenderingIntentDefault); 
    CGContextDrawPDFPage(context, page);
    CGContextRestoreGState(context);

    backgroundImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();
    CGPDFPageRelease(page);

また、CGPDFPageRelease(page); も含めました。dealloc メソッドで。また、小さなドキュメントでは問題なく動作しますが、大きなドキュメントでのみクラッシュすることを追加すると役立つ場合があります. ただし、小さいメモリ リークは依然として発生します。

4

2 に答える 2

0

リリースは、ページが使用される前ではなく、使用された後に行う必要があります。まず、CGPDFPageRelease(page)このコード ブロックの最後に移動して、それが役立つかどうかを確認します。また、問題は変数にCGPDFDocumentRef格納されているものに関係している可能性があります。pdf上記が役に立たない場合は、参照を取得する方法と、それを保持および解放する場所を示すとよいでしょう。

于 2012-05-01T10:38:17.023 に答える