对于PDF阅读器,我想通过拍摄每页的“屏幕截图”来准备文档,并将它们保存到光盘上。第一种方法是
CGPDFDocumentRef document = CGPDFDocumentCreateWithURL((CFURLRef) someURL);
for (int i = 1; i<=pageCount; i++)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
CGPDFPageRef page = CGPDFDocumentGetPage(document, i);
...//getting + manipulating graphics context etc.
...
CGContextDrawPDFPage(context, page);
...
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
...//saving the image to disc
[pool drain];
}
CGPDFDocumentRelease(document);这会导致在第一次运行循环(准备第一个文档)后似乎没有释放大量内存,但在其他运行中不会有更多未释放的内存:
MEMORY BEFORE: 6 MB
MEMORY DURING 1ST DOC: 40 MB
MEMORY AFTER 1ST DOC: 25 MB
MEMORY DURING 2ND DOC: 40 MB
MEMORY AFTER 2ND DOC: 25 MB
....将代码更改为
for (int i = 1; i<=pageCount; i++)
{
CGPDFDocumentRef document = CGPDFDocumentCreateWithURL((CFURLRef) someURL);
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
CGPDFPageRef page = CGPDFDocumentGetPage(document, i);
...//getting + manipulating graphics context etc.
...
CGContextDrawPDFPage(context, page);
...
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
...//saving the image to disc
CGPDFDocumentRelease(document);
[pool drain];
}将内存使用情况更改为
MEMORY BEFORE: 6 MB
MEMORY DURING 1ST DOC: 9 MB
MEMORY AFTER 1ST DOC: 7 MB
MEMORY DURING 2ND DOC: 9 MB
MEMORY AFTER 2ND DOC: 7 MB
....但显然是性能上的倒退。
当我开始阅读PDF (稍后,不同的线程)时,在第一种情况下,没有分配更多的内存(停留在25MB),而在第二种情况下,内存增加到20MB(从7)。
在这两种情况下,当我删除CGContextDrawPDFPage(context, page);时,在所有文档准备过程中和之后,行内存都(几乎)保持在6MB。
有人能解释一下那里发生了什么吗?
发布于 2011-01-13 11:48:07
CGPDFDocument缓存非常强大,除了释放文档并从磁盘重新加载之外,您对此几乎没有控制。
删除CGContextDrawPDFPage调用时看不到大量分配的原因是Quartz延迟加载页面资源。当您只是调用CGPDFDocumentGetPage时,所发生的只是加载一些基本的元数据,比如边界框和注释(在内存中非常小)。
只有当你实际绘制页面时,字体、图像等才会被加载-但之后它们会在内部缓存中保留相对较长的时间。这意味着渲染速度更快,因为页面资源通常在多个页面之间共享。此外,多次渲染一个页面(例如放大时)也是很常见的。您将注意到,第二次呈现页面的速度要快得多。
https://stackoverflow.com/questions/4668772
复制相似问题