Browse Source

Merge branch 'develop' of git.kdan.cc:Mac_PDF/PDF_Office into develop

tangchao 1 year ago
parent
commit
ae039c42a5

+ 26 - 13
PDF Office/PDF Master/Class/PDFTools/ImageToPDF/Manager/KMImageToPDFManager.swift

@@ -139,20 +139,23 @@ class KMImageToPDFManager: NSObject {
         
         //FIXME: 无法插入图片
         let image = NSImage(contentsOfFile: filePath)
-        let insertPageSuccess = document?.insertPage(image!.size, withImage: filePath, at: document!.pageCount)
-        if insertPageSuccess != nil {
-            print("插入成功")
-            //信号量控制异步
-            let semaphore = DispatchSemaphore(value: 0)
-            DispatchQueue.global().async {
-                success = ((document?.write(toFile: path)) != nil)
-                semaphore.signal()
+        if image != nil {
+            let insertPageSuccess = document?.insertPage(image!.size, withImage: filePath, at: document!.pageCount)
+            if insertPageSuccess != nil {
+                print("插入成功")
+                //信号量控制异步
+                let semaphore = DispatchSemaphore(value: 0)
+                DispatchQueue.global().async {
+                    success = ((document?.write(toFile: path)) != nil)
+                    semaphore.signal()
+                }
+                semaphore.wait()
+            } else {
+                print("插入失败")
             }
-            semaphore.wait()
         } else {
             print("插入失败")
         }
-        
         return success
     }
   
@@ -193,11 +196,21 @@ class KMImageToPDFManager: NSObject {
                 if (FileManager.default.fileExists(atPath: imageData.filePath, isDirectory: &isDirectory) &&
                     !isDirectory.boolValue) {
                     let image = NSImage(contentsOfFile: imageData.filePath)
-                    let success = document?.insertPage(image!.size, withImage: imageData.filePath, at: UInt(conversionIndex))
-                    if success! {
-                        imageData.state = .success
+                    if image != nil {
+                        let success = document?.insertPage(image!.size, withImage: imageData.filePath, at: UInt(conversionIndex))
+                        if success! {
+                            imageData.state = .success
+                        } else {
+                            imageData.state = .error
+                            if image?.size.width != 0 && image?.size.height != 0 {
+                                document.insertPage(image!.size, at: UInt(conversionIndex))
+                            } else {
+                                document.insertPage(CGSizeZero, at: UInt(conversionIndex))
+                            }
+                        }
                     } else {
                         imageData.state = .error
+                        document.insertPage(CGSizeZero, at: UInt(conversionIndex))
                     }
                 }
                 conversionIndex += 1