对于PDF阅读器,我希望通过对每个页面的“截图”进行准备并将其保存到光盘。第一种方法是PDF内存分配背后的秘密是什么(CGPDFDocumentRef)
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(在后面的时间,不同的线程)时,不再分配内存(保持在25 MB),而在第二种情况下,内存增加到20 MB(从7)。
在这两种情况下,当我删除CGContextDrawPDFPage(context, page);
行内存时,在所有文档准备期间和之后,内存都是(几乎)恒定在6 MB。
任何人都可以解释发生了什么吗?
谢谢你,对我有意义,所以我把它标记为“正确”而没有实际证明它。 – 2011-01-13 13:52:32