gpt4 book ai didi

iphone - PDF 内存分配背后的 secret 是什么 (tf) (CGPDFDocumentRef)

转载 作者:IT王子 更新时间:2023-10-28 23:38:11 26 4
gpt4 key购买 nike

对于 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(稍后,不同的线程)时,在第一种情况下不再分配内存(保持在 25 MB),而在第二种情况下,内存增加到 20 MB(从 7 开始)。

在这两种情况下,当我删除 CGContextDrawPDFPage(context, page); 在所有文档准备期间和之后,行内存(几乎)恒定为 6 MB。

谁能解释那里发生了什么?

最佳答案

CGPDFDocument 缓存非常积极,除了 - 正如你所做的那样 - 释放文档并从磁盘重新加载它之外,你几乎无法控制它。

删除 CGContextDrawPDFPage 调用时看不到很多分配的原因是 Quartz 延迟加载页面资源。当您只调用 CGPDFDocumentGetPage 时,所发生的只是它会加载一些基本的元数据,例如边界框和注释(内存非常小)。

字体、图像等仅在您实际绘制页面时加载 - 但随后它们会在内部缓存中保留相对较长的时间。这是为了加快渲染速度,因为页面资源通常在多个页面之间共享。此外,多次渲染页面(例如放大时)也很常见。您会注意到第二次渲染页面要快得多。

关于iphone - PDF 内存分配背后的 secret 是什么 (tf) (CGPDFDocumentRef),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4668772/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com