我一直在做研究,因为我的 pdf 查看器应用程序在第 4/5 页左右不断崩溃,因为每次翻页时都会发生内存泄漏。
事实证明,苹果有一个错误。
见这里:https ://devforums.apple.com/message/9077#9077
据我了解,每次更改页面时,您都必须发布并保留 pdf 文档。
我无法让它工作(此代码在 UIView 的子类中):
- (void) drawInContext: (CGContextRef) ctx {
CGPDFDocumentRelease(__pdfDoc);
CGContextSetRGBFillColor( ctx, 1.0, 1.0, 1.0, 1.0 );
CGContextFillRect( ctx, CGContextGetClipBoundingBox( ctx ));
CGContextTranslateCTM( ctx, 0.0, self.bounds.size.height );
CGContextScaleCTM( ctx, 1.0, -1.0 );
CGContextConcatCTM(ctx, CGPDFPageGetDrawingTransform(pdfPage, kCGPDFCropBox, self.bounds, 0, true));
CGContextDrawPDFPage( ctx, pdfPage );NSLog(@"DONE!");
CGPDFDocumentRetain(__pdfDoc);}
@end
我的 pdf 在 viewController 中打开:
- (CGPDFPageRef) pdfPage: (NSInteger) index {
if( ! __pdfDoc ) {
__pdfDoc = CGPDFDocumentCreateWithURL((CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"mybook" ofType:@"pdf"]]);}
if( __pdfDoc ) {
size_t pdfPageCount = CGPDFDocumentGetNumberOfPages( __pdfDoc );
index++;
if( index < 1 )
index = 1;
if( index > pdfPageCount )
index = pdfPageCount;
CGPDFPageRef page = CGPDFDocumentGetPage( __pdfDoc, index );
return page;}
return nil;}
我在谷歌上找不到任何东西,而且我整天都在阅读文档。