// // KMNThumbnailManager.swift // PDF Reader Pro // // Created by 丁林圭 on 2024/10/21. // import Cocoa let kmnThumbnailFolder: String = { let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first! return (documentDirectory as NSString).appendingPathComponent("ThumbnailFolder") }() public class KMNThumbnail: NSObject { var thumbnailDocument:CPDFDocument = CPDFDocument() typealias KMNThumbnailCompletion = (_ image: NSImage?) -> Void var thumbnaiPage:CPDFPage? var pageSize:CGSize = CGSizeZero var pageIndex:Int = 0 var isHoveBookMark:Bool = false public var pageRotate:CGFloat = 0.0 { didSet { } } deinit { print("\(self.className) deinit") } public init(document: CPDFDocument,currentPageIndex:Int) { thumbnailDocument = document pageIndex = currentPageIndex let page = document.page(at: UInt(currentPageIndex)) let size = page?.bounds.size ?? CGSizeZero pageSize = size thumbnaiPage = page pageRotate = 0 isHoveBookMark = document.bookmark(forPageIndex: UInt(pageIndex)) != nil super.init() } public override init() { super.init() } func generateThumImage(completion: KMNThumbnailCompletion?) { let filePathID = KMNThumbnailManager.filePathId(for: thumbnailDocument.documentURL.path) let docsFilePath = kmnThumbnailFolder + "/" + filePathID let fileManager = FileManager.default if !fileManager.fileExists(atPath: docsFilePath) { try? FileManager.default.createDirectory(atPath: docsFilePath, withIntermediateDirectories: true, attributes: nil) } let pageObjNum = thumbnaiPage?.getObjNum() ?? -1 //获取Page的唯一标识码 var imageFilePath = "" if(pageObjNum == -1) { imageFilePath = "" } else { imageFilePath = docsFilePath + "/" + String(pageObjNum) + ".png" } var thumSize = pageSize if(imageFilePath.isEmpty == true) { let maxOrg = max(thumSize.width, thumSize.height) let minOrg = min(thumSize.width, thumSize.height) let maxWidth = 300.0 let maxHeight = 500.0 let scanW = maxWidth / minOrg let scanH = maxHeight / maxOrg let minScanl = min(scanW, scanH) if (thumbnaiPage?.rotation ?? 0) % 180 != 0 { thumSize.width = pageSize.height * minScanl thumSize.height = pageSize.width * minScanl } else { thumSize.width = pageSize.width * minScanl thumSize.height = pageSize.height * minScanl } thumbnaiPage?.thumbnail(of: thumSize, needReset: true,completion: { pageImage in if completion != nil { completion!(pageImage) } // 将 NSImage 转换为 PNG 数据 guard let tiffData = pageImage?.tiffRepresentation, let bitmapImage = NSBitmapImageRep(data: tiffData), let pngData = bitmapImage.representation(using: .png, properties: [:]) else { return } do { try pngData.write(to: URL(fileURLWithPath: imageFilePath)) } catch { } }); } else { if fileManager.fileExists(atPath: imageFilePath) { let image = NSImage.init(contentsOfFile: imageFilePath) ?? NSImage() if completion != nil { completion!(image) } } else { let maxOrg = max(thumSize.width, thumSize.height) let minOrg = min(thumSize.width, thumSize.height) let maxWidth = 300.0 let maxHeight = 500.0 let scanW = maxWidth / minOrg let scanH = maxHeight / maxOrg let minScanl = min(scanW, scanH) if (thumbnaiPage?.rotation ?? 0) % 180 != 0 { thumSize.width = pageSize.height * minScanl thumSize.height = pageSize.width * minScanl } else { thumSize.width = pageSize.width * minScanl thumSize.height = pageSize.height * minScanl } thumbnaiPage?.thumbnail(of: thumSize, needReset: true,completion: { pageImage in if completion != nil { completion!(pageImage) } // 将 NSImage 转换为 PNG 数据 guard let tiffData = pageImage?.tiffRepresentation, let bitmapImage = NSBitmapImageRep(data: tiffData), let pngData = bitmapImage.representation(using: .png, properties: [:]) else { return } do { try pngData.write(to: URL(fileURLWithPath: imageFilePath)) } catch { } }); } } } func removeCacheImage() { let filePathID = KMNThumbnailManager.filePathId(for: thumbnailDocument.documentURL.path) let docsFilePath = kmnThumbnailFolder + "/" + filePathID let fileManager = FileManager.default let pageObjNum = thumbnaiPage?.getObjNum() ?? -1 var imageFilePath = "" if(pageObjNum == -1) { imageFilePath = "" } else { imageFilePath = docsFilePath + "/" + String(pageObjNum) + ".png" } if fileManager.fileExists(atPath: imageFilePath) { try? fileManager.removeItem(atPath: imageFilePath) } } } class KMNThumbnailManager: NSObject { static let manager = KMNThumbnailManager() var copyPages: [CPDFPage] = [] var copyDocument: [CPDFDocument] = [] class func filePathId(for filePath: String) -> String { return "\(filePath.hash)" } class func clearCacheFilePath(filePath:String) { let filePathID = KMNThumbnailManager.filePathId(for: filePath) let docsFilePath = kmnThumbnailFolder + "/" + filePathID let fileManager = FileManager.default if fileManager.fileExists(atPath: docsFilePath) { try? fileManager.removeItem(atPath: docsFilePath) } } class func clearCacheThumImage() { if FileManager.default.fileExists(atPath: kmnThumbnailFolder) { try?FileManager.default.removeItem(atPath: kmnThumbnailFolder) } } }