瀏覽代碼

【综合】提取图片窗口偶发crash修复

tangchao 10 月之前
父節點
當前提交
55f95b48b3

+ 2 - 2
PDF Office/PDF Master/AppDelegate.swift

@@ -39,9 +39,9 @@ class AppDelegate: NSObject, NSApplicationDelegate, iRateDelegate{
         //通知
         UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
             if granted {
-                print("User authorized notifications")
+                KMPrint("User authorized notifications")
             } else {
-                print("User denied notifications")
+                KMPrint("User denied notifications")
             }
         }
 

+ 28 - 13
PDF Office/PDF Master/Class/PDFTools/Convert/ExtractImage/Controller/KMExtractImageWindowController.swift

@@ -42,6 +42,8 @@ class KMExtractImageWindowController: NSWindowController,PDFConvertObjectDelegat
     @IBOutlet var pdfViewBG: NSView!
     var preViewPDFView: CPDFView!
     
+    private var fileAttri_: KMFileAttribute?
+    
     deinit {
         NotificationCenter.default.removeObserver(self)
         
@@ -64,6 +66,9 @@ class KMExtractImageWindowController: NSWindowController,PDFConvertObjectDelegat
             self.preViewPDFView.autoScales = true
             self.preViewPDFView.layoutDocumentView()
             self.preViewPDFView.go(toPageIndex: self.currentPage, animated: true)
+            
+            self.fileAttri_ = KMFileAttribute()
+            self.fileAttri_?.filePath = url.path
         }
         self.allPageButton.title = NSLocalizedString("All Pages", comment: "")
         self.singlePageButton.title = NSLocalizedString("Odd Pages Only", comment: "")
@@ -199,22 +204,26 @@ class KMExtractImageWindowController: NSWindowController,PDFConvertObjectDelegat
                 indeSet.add(IndexSet(integer: IndexSet.Element(index)))
             }
         } else {
-            let fileAttribute = KMFileAttribute()
-            fileAttribute.filePath = self.preViewPDFView.document?.documentURL?.path ?? ""
-            fileAttribute.bAllPage = false
+            var fileAttribute = self.fileAttri_
+            if fileAttribute == nil {
+                self.fileAttri_ = KMFileAttribute()
+                self.fileAttri_?.filePath = self.preViewPDFView.document?.documentURL?.path ?? ""
+                fileAttribute = self.fileAttri_
+            }
+            fileAttribute?.bAllPage = false
             if (self.customPageButton.state == .on){
-                fileAttribute.pagesType = .custom
+                fileAttribute?.pagesType = .custom
             }
-            fileAttribute.pagesString = self.rangeTextField.stringValue
-            if fileAttribute.fetchSelectPages().isEmpty {
+            fileAttribute?.pagesString = self.rangeTextField.stringValue
+            if let data = fileAttribute?.fetchSelectPages().isEmpty, data {
                 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 indeSet
             }
             
-            for num in fileAttribute.fetchSelectPages() {
+            for num in fileAttribute?.fetchSelectPages() ?? [] {
                 indeSet.add(num-1)
             }
         }
@@ -287,11 +296,17 @@ class KMExtractImageWindowController: NSWindowController,PDFConvertObjectDelegat
         }
     }
     func checkPageRangeValidate(_ pageRangeString: String) -> Bool { 
-        let fileAttribute = KMFileAttribute()
-        fileAttribute.filePath = self.preViewPDFView.document?.documentURL?.path ?? ""
-        fileAttribute.bAllPage = false
-        fileAttribute.pagesString = self.rangeTextField.stringValue
-        if fileAttribute.fetchSelectPages().isEmpty || fileAttribute.fetchSelectPages().count < 1{
+        var fileAttribute = self.fileAttri_
+        if fileAttribute == nil {
+            self.fileAttri_ = KMFileAttribute()
+            self.fileAttri_?.filePath = self.preViewPDFView.document?.documentURL?.path ?? ""
+            fileAttribute = self.fileAttri_
+        }
+        fileAttribute?.bAllPage = false
+        fileAttribute?.pagesString = self.rangeTextField.stringValue
+        let isEmpty = fileAttribute?.fetchSelectPages().isEmpty ?? true
+        let cnt = fileAttribute?.fetchSelectPages().count ?? 0
+        if isEmpty || cnt < 1{
             return false
         }
         return true

+ 1 - 1
PDF Office/PDF Master/Class/PDFWindowController/Toolbar/KMToolbarItemView.swift

@@ -641,7 +641,7 @@ extension KMToolbarItemView {
             self.imageViewBox.km_add_inset_constraint()
             
             self.imageViewBox.contentView?.addSubview(self.imageViewBtn)
-            self.imageViewBtn.km_add_inset_constraint(equalTo: self.imageViewBox, inset: NSEdgeInsets(top: offsetY, left: offsetX, bottom: offsetY, right: offsetX))
+            self.imageViewBtn.km_add_inset_constraint(equalTo: self.imageViewBox, inset: NSEdgeInsets(top: offsetY, left: 0, bottom: offsetY, right: 0))
             self.itemHeight = 24
         } else if (self.boxImagePosition == .imageLeft) {
             self.addSubview(self.imageViewBox)

+ 2 - 2
PDF Office/PDF Master/Class/PDFWindowController/Toolbar/KMToolbarViewController.swift

@@ -1704,7 +1704,7 @@ extension KMToolbarViewController {
             item?.btnAction = #selector(convertToImage)
             item?.promptIdentifier = identifier
             
-            let menuItem = NSMenuItem.init(title: "To Image", action: #selector(conversionMenuItemAction), target: self)
+            let menuItem = NSMenuItem.init(title: item?.titleName ?? "", action: #selector(conversionMenuItemAction), target: self)
             let subMenu = NSMenu()
             subMenu.addItem(title: NSLocalizedString("PDF To JPEG", comment: ""), action: #selector(_pdfToImageAction), target: self, tag: KMPDFConvertType.jpeg.rawValue)
             subMenu.addItem(title: NSLocalizedString("PDF To JPG", comment: ""), action: #selector(_pdfToImageAction), target: self, tag: KMPDFConvertType.jpg.rawValue)
@@ -1725,7 +1725,7 @@ extension KMToolbarViewController {
             item?.needExpandAction = true
             item?.promptIdentifier = identifier
             
-            let menuItem = NSMenuItem.init(title: "To Image", action: nil, target: self)
+            let menuItem = NSMenuItem.init(title: item?.titleName ?? "", action: nil, target: self)
             let subMenu = NSMenu()
             subMenu.addItem(title: NSLocalizedString("Extract All Images", comment: ""), action: #selector(_exportImageAction), target: self, tag: 1)
             subMenu.addItem(title: NSLocalizedString("Extract Images", comment: ""), action: #selector(_exportImageAction), target: self, tag: 2)