Browse Source

【综合】水印偶发crash修复

tangchao 1 year ago
parent
commit
b70b8b6ed6

+ 31 - 16
PDF Office/PDF Master/Class/PDFTools/Watermark/New/View/KMWatermarkView.swift

@@ -98,6 +98,7 @@ class KMWatermarkView: KMBaseXibView, NSTextFieldDelegate, NSComboBoxDelegate, N
     var pdfDocument: CPDFDocument? {
         didSet {
             self._fileAttri = KMFileAttribute()
+            self._fileAttri?.password = self.pdfDocument?.password ?? ""
             self._fileAttri?.filePath = self.pdfDocument?.documentURL?.path ?? ""
             self.password = pdfDocument?.password ?? ""
             DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
@@ -446,11 +447,16 @@ class KMWatermarkView: KMBaseXibView, NSTextFieldDelegate, NSComboBoxDelegate, N
     }
     
     func checkPageRangeValidate(_ pageRangeString: String) -> Bool {
-        let fileAttribute = KMFileAttribute()
-        fileAttribute.filePath = (self.pdfDocument?.documentURL.path)!
-        fileAttribute.password = self.pdfDocument?.password ?? ""
-        fileAttribute.bAllPage = false
-        fileAttribute.pagesString = self.pageRangeComboBox.stringValue
+        var fileAttribute = self._fileAttri
+        if fileAttribute == nil {
+            fileAttribute = KMFileAttribute()
+            fileAttribute?.password = self.pdfDocument?.password ?? ""
+            fileAttribute?.filePath = self.pdfDocument?.documentURL.path ?? ""
+            self._fileAttri = fileAttribute
+        }
+        
+        fileAttribute?.bAllPage = false
+        fileAttribute?.pagesString = self.pageRangeComboBox.stringValue
         
         var pageRange: KMPageRange = .all
         let pageRangeType: KMWatermarkeModelPageRangeType = watermark.pageRangeType
@@ -464,9 +470,9 @@ class KMWatermarkView: KMBaseXibView, NSTextFieldDelegate, NSComboBoxDelegate, N
             pageRange = .custom
         }
          
-        fileAttribute.pagesType = pageRange
+        fileAttribute?.pagesType = pageRange
         
-        if fileAttribute.fetchSelectPages().count == 0 {
+        if let cnt = fileAttribute?.fetchSelectPages().count, cnt == 0 {
             return false
         }
         return true
@@ -782,20 +788,25 @@ extension KMWatermarkView {
         case 2:
             pages = Array(stride(from: 1, to: Int(pdfDocument.pageCount), by: 2))
         default:
-            let fileAttribute = KMFileAttribute()
-            fileAttribute.filePath = pdfDocument.documentURL?.path ?? ""
-            fileAttribute.bAllPage = false
-            fileAttribute.pagesType = .custom
-            fileAttribute.password = pdfDocument.password ?? ""
-            fileAttribute.pagesString = pageRangeComboBox.stringValue
+            var fileAttribute = self._fileAttri
+            if fileAttribute == nil {
+                fileAttribute = KMFileAttribute()
+                fileAttribute?.password = pdfDocument.password ?? ""
+                fileAttribute?.filePath = pdfDocument.documentURL?.path ?? ""
+                self._fileAttri = fileAttribute
+            }
+            fileAttribute?.bAllPage = false
+            fileAttribute?.pagesType = .custom
+            
+            fileAttribute?.pagesString = pageRangeComboBox.stringValue
 
-            let selectPages = fileAttribute.fetchSelectPages()
+            let selectPages = fileAttribute?.fetchSelectPages() ?? []
             if selectPages.count != 0 {
                 pages = selectPages.map { $0 - 1 }
             } else {
                 let alert = NSAlert()
                 alert.alertStyle = .critical
-                alert.messageText = "\(fileAttribute.filePath.lastPathComponent) \(NSLocalizedString("Invalid page range or the page number is out of range. Please try again.", comment: ""))"
+                alert.messageText = "\(fileAttribute?.filePath.lastPathComponent ?? "") \(NSLocalizedString("Invalid page range or the page number is out of range. Please try again.", comment: ""))"
                 alert.runModal()
                 return
             }
@@ -1022,14 +1033,18 @@ extension KMWatermarkView {
             let wm = KMWatermarkView()
             wm.pdfView = KMWatermarkPDFView()
             wm.pdfView.document = document
+            wm.password = document.password ?? ""
             wm.addWatermark(model: model, toPath: toPath, completion: completion)
         }
     }
     func addWatermark(model: KMWatermarkModel, toPath: String, completion: @escaping (_ result: Bool) -> ()) {
         DispatchQueue.global().async {
             let waterDocument = CPDFDocument(url: self.pdfView.document.documentURL)
+
             guard let waterDocumentT = waterDocument else { return }
-            waterDocumentT.unlock(withPassword: self.password)
+            if self.password.isEmpty == false {
+                waterDocumentT.unlock(withPassword: self.password)
+            }
             var property: CPDFWatermark!
             var scale: CGFloat = model.scale
             if (!model.text.isEmpty) {