CGContextDrawPDFPage内存泄露

问题描述:

你好,这里是我在CATiledlayer绘制PDF的代码CGContextDrawPDFPage内存泄露

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx 
{ 

     CGContextSetRGBFillColor(ctx, 1.0, 1.0, 1.0, 1.0); 
     CGContextFillRect(ctx, CGContextGetClipBoundingBox(ctx)); 
     CGContextTranslateCTM(ctx, 0.0, layer.bounds.size.height); 
     CGContextScaleCTM(ctx, 1.0, -1.0); 
     CGContextConcatCTM(ctx, CGPDFPageGetDrawingTransform(myPageRef, kCGPDFCropBox, layer.bounds, 0, true)); 
     CGContextDrawPDFPage(ctx, myPageRef); 
} 

一切都很好,但我在下面一行

 CGContextDrawPDFPage(ctx, myPageRef); 

了内存泄漏警告这里myPageRef是CGPDFPageRef

我已经从github上下载了代码,并做了一些R & D,并发现,

我忘了释放CGPDFPageRelease(myPageRef)我TiledView的dealloc方法..

和编写这些代码我的内存泄漏解决后....

// Clean up. 

- (void)dealloc { 
    CGPDFPageRelease(myPageRef); 
    [super dealloc]; 
} 
+0

你应该只能做,如果你保留它在某个时候。但是如果你只是在做'CGPDFDocumentGetPage',你会得到一个autorelease对象,因此你不应该释放它。 (显然,如果你保留它,那么当然你必须按照这个答案中的建议释放它)。 – Rob 2013-07-17 14:07:15

+0

https://stackoverflow.com/questions/46903182/cgcontextdrawpdfpage-memory-leak-app-crash – Ravindhiran 2017-10-24 06:26:49

调用

CGContextSetInterpolationQuality(context, kCGInterpolationHigh); 
CGContextSetRenderingIntent(context, kCGRenderingIntentDefault); 
CGContextDrawPDFPage

解决我的一个类似问题。

学分转到这个答案约翰的: CGContextDrawPDFPage taking up large amounts of memory