【问题标题】:PDFKit memory issues ios12PDFKit内存问题ios12
【发布时间】:2018-02-23 23:45:24
【问题描述】:

编辑了这个问题以使用更简单的代码版本。 TestPDF 全是文本,大约 300 页。当循环运行时,它会在消耗 2gb 内存后崩溃。打印后我不需要 print 语句中的值。但是,代码将其保存在内存中。如何在循环关闭前清除打印语句内容的内存分配?

func loadPDFDocument(){
        let documentURL = Bundle.main.url(forResource: "TestPDF", withExtension: "pdf")!

        if let document = PDFDocument(url: documentURL) {

            for page in 1...document.pageCount {
                DispatchQueue.global().async {
                print(document.page(at: page)!.string!)
                }
            }

        }

    }

我尝试过的解决方案包括 autoreleasepool 并在每个循环中创建一个新的 PDFDocument 对象并使用它。第二个选项确实释放了内存,但速度太慢了。

 func loadPDFDocument(){
        let documentURL   = Bundle.main.url(forResource: "TestPDF", withExtension: "pdf")!

     if let document      = PDFDocument(url: documentURL) {



            for page in 1...document.pageCount {
                 DispatchQueue.global().async {
                  let innerDocument = PDFDocument(url: documentURL)!
                     print(innerDocument.page(at: page)!.string!)
                    }
              }

            }

        }

【问题讨论】:

  • 你解决了吗???
  • @JimBak 没有。我在 Apple 开发者论坛上也没有任何回应。我已经开始研究我的应用程序的其他领域。当我在提交之前重新审视这个问题时,我很可能会使用我的开发人员会员资格的代码支持之一
  • 感谢您告诉我们!
  • 您是否使用过 Instruments 来查看问题所在?

标签: memory-management swift4 pdfkit


【解决方案1】:

到目前为止,我的解决方案是在didReceiveMemoryWarning 中重新加载 PDFDocument

所以我有一个全局变量

 var document =  PDFDocument()

使用它

let pdfURL   = ...
     document =   PDFDocument(url: pdfURL)! 

如果内存不足

 override func didReceiveMemoryWarning() {
    let pdfURL   = ...
        document =  PDFDocument(url: pdfURL)! 
}

【讨论】:

  • 我的问题是在一个大的 PDF 中,只要用户滚动导致页面加载 - 内存增加。我的解决方案与您的类似,捕获内存警告并重新加载 PDF,确保跳转到用户最后看到的页面。这个想法是在我试图了解 Apple 为克服这个问题所做的工作时想到的,所以我在 safari 中打开了 PDF 并开始滚动。我看到他们如何重新加载 PDF,所以我也照做了。我想知道为什么这么长时间都没有修复!
猜你喜欢
  • 2019-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-03
  • 2019-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多