浏览代码

【合并】附件文档合并时,APP会无响应,内存一直增加 --更换实现方式

lizhe 6 月之前
父节点
当前提交
d25b9dae1d
共有 1 个文件被更改,包括 37 次插入37 次删除
  1. 37 37
      PDF Office/PDF Master/Class/PDFTools/Merge/MergeNew/KMMergeWindowController.swift

+ 37 - 37
PDF Office/PDF Master/Class/PDFTools/Merge/MergeNew/KMMergeWindowController.swift

@@ -18,12 +18,13 @@ class KMMergeWindowController: KMBaseWindowController {
     
 //    var cancelAction: KMMergeWindowControllerCancelAction?
     
-    var oldPDFDocument: PDFDocument = PDFDocument()
+    var oldPDFDocument: CPDFDocument = CPDFDocument()
     var password: String = ""
     
     var oriDucumentUrl: URL? {
         didSet {
-            oldPDFDocument = PDFDocument(url: oriDucumentUrl!)!
+            guard let document = CPDFDocument(url: oriDucumentUrl!) else { return }
+            oldPDFDocument = document
             oldPDFDocument.unlock(withPassword: self.password)
         }
     }
@@ -158,7 +159,7 @@ extension KMMergeWindowController {
 //        [self.nCancelVC setEnabled:NO];
 //        self.canMerge = NO;
 //        
-        var rootPDFOutlineArray: [PDFOutline] = []
+        var rootPDFOutlineArray: [CPDFOutline] = []
         var allPage = true //只有是全部才支持大纲的合并
         for file in files {
             if file.fetchSelectPages().count == 0 {
@@ -172,7 +173,7 @@ extension KMMergeWindowController {
             allPage = file.bAllPage
             
             /*防止文件被地址变换后crash*/
-            guard let tDocument = PDFDocument(url: NSURL(fileURLWithPath: file.filePath) as URL) else {
+            guard let tDocument = CPDFDocument(url: NSURL(fileURLWithPath: file.filePath) as URL) else {
                 print("文件不存在")
                 let alert = NSAlert.init()
                 alert.alertStyle = .critical
@@ -181,31 +182,30 @@ extension KMMergeWindowController {
                 return
             }
             
-            var outlineArray: [PDFOutline] = []
+            var outlineArray: [CPDFOutline] = []
             
 //            if file.isLocked {
                 tDocument.unlock(withPassword: file.password)
 //            }
             
-            if tDocument.outlineRoot != nil {
-                rootPDFOutlineArray.append((tDocument.outlineRoot)!)
-                self.fetchAllOfChildren((tDocument.outlineRoot)!, containerArray: &outlineArray)
-                outlineArray.removeObject((tDocument.outlineRoot)!)
+            if tDocument.outlineRoot() != nil {
+                rootPDFOutlineArray.append(tDocument.outlineRoot())
+                self.fetchAllOfChildren(tDocument.outlineRoot(), containerArray: &outlineArray)
+                outlineArray.removeObject(tDocument.outlineRoot())
             } else {
-                let rootOutline = PDFOutline.init()
-                tDocument.outlineRoot = rootOutline
-                if tDocument.outlineRoot != nil {
-                    rootPDFOutlineArray.append(tDocument.outlineRoot!)
+                let rootOutline = tDocument.setNewOutlineRoot()
+                if rootOutline != nil {
+                    rootPDFOutlineArray.append(rootOutline!)
                 }
             }
             
             for number in file.fetchSelectPages() {
-                let page = tDocument.page(at: number - 1)
+                let page = tDocument.page(at: UInt(number - 1))
 //                if pageIndex != nil {
 //                    self.oldPDFDocument.insert(page!, at: pageIndex!)
 //                    pageIndex = pageIndex! + 1
 //                } else {
-                    self.oldPDFDocument.insert(page!, at: self.oldPDFDocument.pageCount)
+                self.oldPDFDocument.insertPageObject(page, at: self.oldPDFDocument.pageCount)
 //                }
 //                self.insertIndexSet.addIndex:(self.pdfDocument.pageCount - 1)
             }
@@ -214,15 +214,15 @@ extension KMMergeWindowController {
         
         let fileName = (files.first?.filePath.deletingPathExtension.lastPathComponent ?? "") + "_Merged"
         DispatchQueue.main.async {
-            if self.oldPDFDocument.outlineRoot == nil {
-                self.oldPDFDocument.outlineRoot = PDFOutline.init()
+            if self.oldPDFDocument.outlineRoot() == nil {
+                self.oldPDFDocument.setNewOutlineRoot()
             }
 //            if allPage {
                 var insertIndex = 0
                 for i in 0..<rootPDFOutlineArray.count {
                     let rootOutline = rootPDFOutlineArray[i]
                     for j in 0..<rootOutline.numberOfChildren {
-                        self.oldPDFDocument.outlineRoot?.insertChild(rootOutline.child(at: j)!, at: insertIndex)
+                        self.oldPDFDocument.outlineRoot().insertChild(rootOutline.child(at: j)!, at: UInt(insertIndex))
                         insertIndex = insertIndex + 1
                     }
                 }
@@ -232,7 +232,7 @@ extension KMMergeWindowController {
                 if self.oriDucumentUrl != nil {
                     
                     let newPath = self.oldPDFDocument.documentURL!.path
-                    var options: [PDFDocumentWriteOption : Any] = [:]
+                    var options: [CPDFDocumentWriteOption : Any] = [:]
                     var success = false
                     let password = self.password
                     let pdf = self.oldPDFDocument
@@ -326,7 +326,7 @@ extension KMMergeWindowController {
         }
     }
     
-    func fetchAllOfChildren(_ aOutline: PDFOutline, containerArray aMArray: inout [PDFOutline]) {
+    func fetchAllOfChildren(_ aOutline: CPDFOutline, containerArray aMArray: inout [CPDFOutline]) {
         if !aMArray.contains(aOutline) {
             aMArray.append(aOutline)
         }
@@ -348,7 +348,7 @@ extension KMMergeWindowController {
                 return
             }
             
-            var pagesArray: [PDFPage] = []
+            var pagesArray: [CPDFPage] = []
             let pageCount = self.oldPDFDocument.pageCount
             for i in 0..<pageCount {
                 pagesArray.append(self.oldPDFDocument.page(at: 0)!)
@@ -358,16 +358,16 @@ extension KMMergeWindowController {
             for i in 0..<pageCount {
                 let page: KMMergePDFPage = KMMergePDFPage.init()
                 page.setBounds(CGRectMake(0, 0, size.width, size.height), for: .mediaBox)
-                page.drawingPage = pagesArray[i]
-                self.oldPDFDocument.insert(page, at: i)
+                page.drawingPage = pagesArray[Int(i)]
+                self.oldPDFDocument.insertPageObject(page, at: i)
             }
             
-            if self.oldPDFDocument.outlineRoot != nil {
-                let childCount = self.oldPDFDocument.outlineRoot?.numberOfChildren
-                var outlineArray: [PDFOutline] = []
+            if self.oldPDFDocument.outlineRoot() != nil {
+                let childCount = self.oldPDFDocument.outlineRoot().numberOfChildren
+                var outlineArray: [CPDFOutline] = []
                 
-                for i in 0..<childCount! {
-                    outlineArray.append((self.oldPDFDocument.outlineRoot?.child(at: i))!)
+                for i in 0..<childCount {
+                    outlineArray.append(self.oldPDFDocument.outlineRoot().child(at: i))
                 }
                 
                 for outline in outlineArray {
@@ -378,17 +378,17 @@ extension KMMergeWindowController {
     }
 }
 
-class KMMergePDFPage: PDFPage {
-    var drawingPage: PDFPage?
+class KMMergePDFPage: CPDFPage {
+    var drawingPage: CPDFPage?
     
-    override func draw(with box: PDFDisplayBox, to context: CGContext) {
+    override func draw(with box: CPDFDisplayBox, to context: CGContext!) {
         super.draw(with: box, to: context)
-        
+     
         let pageSize = self.bounds(for: .cropBox).size
         self.drawPage(with: context, page: self.drawingPage!, pageSize: pageSize)
     }
-    
-    func drawPage(with context: CGContext, page: PDFPage, pageSize: CGSize) {
+
+    func drawPage(with context: CGContext, page: CPDFPage, pageSize: CGSize) {
         var originalSize = page.bounds(for: .cropBox).size
         
         // 如果页面的旋转角度为90或者270,宽高交换
@@ -408,13 +408,13 @@ class KMMergePDFPage: PDFPage {
         
         if #available(macOS 10.12, *) {
             page.draw(with: .cropBox, to: context)
-            page.transformContext(for: .cropBox)
+            page.transform(context, for: .cropBox)
         } else {
             NSGraphicsContext.saveGraphicsState()
             NSGraphicsContext.current = NSGraphicsContext(cgContext: context, flipped: false)
-            page.draw(with: .cropBox)
+            page.draw(with: .cropBox, to: context)
             NSGraphicsContext.restoreGraphicsState()
-            page.transformContext(for: .cropBox)
+            page.transform(context, for: .cropBox)
         }
         
         context.restoreGState()