PDFKit 内存问题 ios12 [英] PDFKit memory issues ios12

查看:54
本文介绍了PDFKit 内存问题 ios12的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Edited this question to use a simpler version of the code. The TestPDF is all text and about 300 pages. As the loop runs it crashes after consuming 2gb of memory. I don’t need the value in the print statement after it’s printed. However the code keeps it in memory. How to clear the memory allocation of the contents of the print statement before the loop closes?

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 对象并使用它.第二个选项确实释放了内存,但速度太慢了.

Solutions I have tried include autoreleasepool and creating a new PDFDocument object in for each loop and using that. That second option does free the memory but is seriously too slow.

 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!)
                    }
              }

            }

        }

推荐答案

到目前为止,我的解决方案是在 didReceiveMemoryWarning

my solution so far has been to reload the PDFDocument in didReceiveMemoryWarning

所以我有一个全局变量

 var document =  PDFDocument()

使用它

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

如果内存不足

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

这篇关于PDFKit 内存问题 ios12的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆