Procházet zdrojové kódy

综合 - 修复【从图片开启PDF】不开启批量弹窗,多选图片开启多张弹窗

wanjun před 2 roky
rodič
revize
53169c8bf1

+ 4 - 0
PDF Office/PDF Office/Class/Home/Category/String+KMExtensions.swift

@@ -32,6 +32,10 @@ extension String {
         return fileURL.deletingPathExtension().path
     }
     
+    var deletingLastPathComponent: String {
+        return fileURL.deletingLastPathComponent().path
+    }
+    
     func stringByAppendingPathExtension(_ pathExtension: String) -> String {
         return fileURL.appendingPathExtension(pathExtension).path
     }

+ 34 - 19
PDF Office/PDF Office/Class/Home/ViewController/KMHomeViewController+Action.swift

@@ -192,7 +192,7 @@ extension KMHomeViewController {
                                 (type == "pict") ||
                                 (type == "sgi") ||
                                 (type == "heic") {
-                        imageUrl.append(url)
+                        openImageFile(url: url)
                     } else if (type == "doc") ||
                                 (type == "docx") ||
                                 (type == "xls") ||
@@ -203,12 +203,6 @@ extension KMHomeViewController {
                         self.openOfficeFile(url: url)
                     }
                 }
-                if imageUrl.count > 0 {
-                    let imageToPDFWindow: KMImageToPDFWindowController = KMImageToPDFWindowController.init(windowNibName: "KMImageToPDFWindowController")
-                    imageToPDFWindow.showWindow(NSApp.mainWindow)
-                    imageToPDFWindow.batchPrecessingView.inputData = imageUrl
-                    imageToPDFWindow.inputType = .ImageToPDF
-                }
             }
         }
     }
@@ -406,10 +400,7 @@ extension KMHomeViewController {
                   (type == "pict") ||
                   (type == "sgi") ||
                   (type == "heic") {
-            let imageToPDFWindow: KMImageToPDFWindowController = KMImageToPDFWindowController.init(windowNibName: "KMImageToPDFWindowController")
-            imageToPDFWindow.showWindow(NSApp.mainWindow)
-            imageToPDFWindow.batchPrecessingView.inputData = [path]
-            imageToPDFWindow.inputType = .ImageToPDF
+            openImageFile(url: path)
         } else if (type == "doc") ||
                   (type == "docx") ||
                   (type == "xls") ||
@@ -420,18 +411,42 @@ extension KMHomeViewController {
             let fileName: NSString = String(format: "%@.pdf", NSLocalizedString("Untitled", comment: "")) as NSString
             let savePath = fetchUniquePath(fileName.kUrlToPDFFolderPath())
             openOfficeFile(url: path)
-//            CPDFKit.sharedInstance().convertFilePath(path.path, toPath: savePath) { success in
-//                if success {
-//                    NSDocumentController.shared.openDocument(withContentsOf: URL(fileURLWithPath: savePath), display: true) { document, documentWasAlreadyOpen, error in
-//
-//                    }
-//
-//                }
-//            }
         }
 
     }
     
+    func openImageFile(url: URL) -> Void {
+        let filePath = url.path
+        
+        let fileName: NSString = url.lastPathComponent as NSString
+        let savePath = fetchUniquePath(fileName.kUrlToPDFFolderPath()).deletingLastPathComponent
+
+        let imageName = NSString(string: NSString(string: filePath).lastPathComponent).deletingPathExtension
+        
+        let path = self.fetchDifferentFilePath(filePath: savePath + "/" + imageName + ".pdf")
+        let document = CPDFDocument.init()
+        var success = false
+        
+        //FIXME: 无法插入图片
+        let image = NSImage(contentsOfFile: filePath)
+        let insertPageSuccess = document?.insertPage(image!.size, withImage: filePath, at: document!.pageCount)
+        if insertPageSuccess != nil {
+            //信号量控制异步
+            let semaphore = DispatchSemaphore(value: 0)
+            DispatchQueue.global().async {
+                success = ((document?.write(toFile: path)) != nil)
+                semaphore.signal()
+            }
+            semaphore.wait()
+        } else {
+        }
+        if success {
+            NSDocumentController.shared.openDocument(withContentsOf: URL(fileURLWithPath: path), display: true) { document, documentWasAlreadyOpen, error in
+
+            }
+        }
+    }
+    
     func openOfficeFile(url: URL) -> Void {
         let filePath = url.path
         let folderPath = "convertToPDF.pdf"

+ 12 - 0
PDF Office/PDF Office/Class/Home/ViewController/KMHomeViewController.swift

@@ -380,6 +380,18 @@ import KMAdvertisement
             self.product_active_spacing.constant = 24.0
         }
     }
+    
+    func fetchDifferentFilePath(filePath: String) -> String {
+        var resultFilePath = filePath
+        var index: Int = 0
+        while (FileManager.default.fileExists(atPath: resultFilePath)) {
+            index += 1
+            let path = NSString(string: filePath).deletingPathExtension + "(" + String(index) + ")"
+            resultFilePath = NSString(string: path).appendingPathExtension(NSString(string: filePath).pathExtension)!
+        }
+        
+        return resultFilePath;
+    }
 
     // MARK: Common methods