Pārlūkot izejas kodu

【文字图片编辑】导入图片分辨率限制

lizhe 1 gadu atpakaļ
vecāks
revīzija
21a02e1762

+ 31 - 0
PDF Office/PDF Master/Class/PDFWindowController/Side/RightSide/EditPDF/Category/KMImageOptimization.swift

@@ -104,6 +104,25 @@ class KMImageOptimization: NSObject {
         return compressedData
     }
     
+    static func resizeImage(_ image: NSImage, toSize newSize: NSSize) -> Data? {
+        let resizedImage = NSImage(size: newSize)
+
+        resizedImage.lockFocus()
+        NSGraphicsContext.current?.imageInterpolation = .high
+
+        image.draw(in: NSRect(x: 0, y: 0, width: newSize.width, height: newSize.height), from: NSRect.zero, operation: .copy, fraction: 1.0)
+
+        resizedImage.unlockFocus()
+
+        guard let imageData = resizedImage.tiffRepresentation,
+              let bitmapImage = NSBitmapImageRep(data: imageData),
+              let compressedData = bitmapImage.representation(using: .png, properties: [:]) else {
+            return nil
+        }
+
+        return compressedData
+    }
+    
     static func needCompressImageLosslessly(image: NSImage, targetSize: CGSize, maxSizeInBytes: Int, targetCompression: CGFloat) -> String {
         let kFilePath: NSString = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first!.path + "KMImageOptimizationCache" as NSString
         let string: NSString = (kFilePath as String) + "/" + "optimization.png" as NSString
@@ -126,6 +145,18 @@ class KMImageOptimization: NSObject {
                 print("保存图像失败:\(error)")
             }
         }
+        
+//        // 示例用法
+//        if let compressedData = KMImageOptimization.resizeImage(image, toSize: targetSize) {
+//            let saveURL = URL(fileURLWithPath: string as String)//URL(fileURLWithPath: "/path/to/save/compressed_image.png")
+//            do {
+//                try compressedData.write(to: saveURL)
+//                // 图像已成功保存在指定路径中
+//                print("保存图像成功:\(string)")
+//            } catch {
+//                print("保存图像失败:\(error)")
+//            }
+//        }
         return string as String
     }
 }

+ 4 - 4
PDF Office/PDF Master/Class/PDFWindowController/ViewController/KMMainViewController.swift

@@ -1416,7 +1416,7 @@ import Cocoa
                         var previewSize = rect.size
                         var isChangeSize = false
                         if previewSize.width == 0 && previewSize.height == 0 {
-                            previewSize = CGSize(width: 400, height: 400)
+                            previewSize = CGSize(width: 500, height: 500)
                             isChangeSize = true
                         }
                         
@@ -1432,10 +1432,10 @@ import Cocoa
                         }
                         
                         imageRect.size = newSize
-                        
-                        if imageSize.width > 1080 || imageSize.height > 1080 {
+                        let limitWidth = 1920.0
+                        if imageSize.width > limitWidth || imageSize.height > limitWidth {
                             filePath = KMImageOptimization.needCompressImageLosslessly(image: image!,
-                                                                                       targetSize: CGSize(width: 1080, height: 1080),
+                                                                                       targetSize: CGSize(width: limitWidth, height: limitWidth),
                                                                                        maxSizeInBytes: 1024 * 1024 * 5,
                                                                                        targetCompression: 1.0)
                         }