KMNThumbnailManager.swift 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. //
  2. // KMNThumbnailManager.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by 丁林圭 on 2024/10/21.
  6. //
  7. import Cocoa
  8. let kmnThumbnailFolder: String = {
  9. let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
  10. return (documentDirectory as NSString).appendingPathComponent("ThumbnailFolder")
  11. }()
  12. public class KMNThumbnail: NSObject {
  13. var thumbnailDocument:CPDFDocument = CPDFDocument()
  14. typealias KMNThumbnailCompletion = (_ image: NSImage?) -> Void
  15. var thumbnaiPage:CPDFPage?
  16. var pageSize:CGSize = CGSizeZero
  17. var pageIndex:Int = 0
  18. var isHoveBookMark:Bool = false
  19. public var pageRotate:CGFloat = 0.0 {
  20. didSet {
  21. }
  22. }
  23. deinit {
  24. print("\(self.className) deinit")
  25. }
  26. public init(document: CPDFDocument,currentPageIndex:Int) {
  27. thumbnailDocument = document
  28. pageIndex = currentPageIndex
  29. let page = document.page(at: UInt(currentPageIndex))
  30. let size = page?.bounds.size ?? CGSizeZero
  31. pageSize = size
  32. thumbnaiPage = page
  33. pageRotate = 0
  34. isHoveBookMark = document.bookmark(forPageIndex: UInt(pageIndex)) != nil
  35. super.init()
  36. }
  37. public override init() {
  38. super.init()
  39. }
  40. func generateThumImage(completion: KMNThumbnailCompletion?) {
  41. let filePathID = KMNThumbnailManager.filePathId(for: thumbnailDocument.documentURL.path)
  42. let docsFilePath = kmnThumbnailFolder + "/" + filePathID
  43. let fileManager = FileManager.default
  44. if !fileManager.fileExists(atPath: docsFilePath) {
  45. try? FileManager.default.createDirectory(atPath: docsFilePath, withIntermediateDirectories: true, attributes: nil)
  46. }
  47. // let pageObjNum = thumbnaiPage?.getObjNum() ?? -1 //获取Page的唯一标识码
  48. let pageObjNum = 1000
  49. var imageFilePath = ""
  50. if(pageObjNum == -1) {
  51. imageFilePath = ""
  52. } else {
  53. imageFilePath = docsFilePath + "/" + String(pageObjNum) + ".png"
  54. }
  55. var thumSize = pageSize
  56. if(imageFilePath.isEmpty == true) {
  57. let maxOrg = max(thumSize.width, thumSize.height)
  58. let minOrg = min(thumSize.width, thumSize.height)
  59. let maxWidth = 300.0
  60. let maxHeight = 500.0
  61. let scanW = maxWidth / minOrg
  62. let scanH = maxHeight / maxOrg
  63. let minScanl = min(scanW, scanH)
  64. if (thumbnaiPage?.rotation ?? 0) % 180 != 0 {
  65. thumSize.width = pageSize.height * minScanl
  66. thumSize.height = pageSize.width * minScanl
  67. } else {
  68. thumSize.width = pageSize.width * minScanl
  69. thumSize.height = pageSize.height * minScanl
  70. }
  71. thumbnaiPage?.thumbnail(of: thumSize, needReset: true,completion: { pageImage in
  72. if completion != nil {
  73. completion!(pageImage)
  74. }
  75. // 将 NSImage 转换为 PNG 数据
  76. guard let tiffData = pageImage?.tiffRepresentation,
  77. let bitmapImage = NSBitmapImageRep(data: tiffData),
  78. let pngData = bitmapImage.representation(using: .png, properties: [:]) else {
  79. return
  80. }
  81. do {
  82. try pngData.write(to: URL(fileURLWithPath: imageFilePath))
  83. } catch {
  84. }
  85. });
  86. } else {
  87. if fileManager.fileExists(atPath: imageFilePath) {
  88. let image = NSImage.init(contentsOfFile: imageFilePath) ?? NSImage()
  89. if completion != nil {
  90. completion!(image)
  91. }
  92. } else {
  93. let maxOrg = max(thumSize.width, thumSize.height)
  94. let minOrg = min(thumSize.width, thumSize.height)
  95. let maxWidth = 300.0
  96. let maxHeight = 500.0
  97. let scanW = maxWidth / minOrg
  98. let scanH = maxHeight / maxOrg
  99. let minScanl = min(scanW, scanH)
  100. if (thumbnaiPage?.rotation ?? 0) % 180 != 0 {
  101. thumSize.width = pageSize.height * minScanl
  102. thumSize.height = pageSize.width * minScanl
  103. } else {
  104. thumSize.width = pageSize.width * minScanl
  105. thumSize.height = pageSize.height * minScanl
  106. }
  107. thumbnaiPage?.thumbnail(of: thumSize, needReset: true,completion: { pageImage in
  108. if completion != nil {
  109. completion!(pageImage)
  110. }
  111. // 将 NSImage 转换为 PNG 数据
  112. guard let tiffData = pageImage?.tiffRepresentation,
  113. let bitmapImage = NSBitmapImageRep(data: tiffData),
  114. let pngData = bitmapImage.representation(using: .png, properties: [:]) else {
  115. return
  116. }
  117. do {
  118. try pngData.write(to: URL(fileURLWithPath: imageFilePath))
  119. } catch {
  120. }
  121. });
  122. }
  123. }
  124. }
  125. func removeCacheImage() {
  126. let filePathID = KMNThumbnailManager.filePathId(for: thumbnailDocument.documentURL.path)
  127. let docsFilePath = kmnThumbnailFolder + "/" + filePathID
  128. let fileManager = FileManager.default
  129. // let pageObjNum = thumbnaiPage?.getObjNum() ?? -1
  130. let pageObjNum = 1000
  131. var imageFilePath = ""
  132. if(pageObjNum == -1) {
  133. imageFilePath = ""
  134. } else {
  135. imageFilePath = docsFilePath + "/" + String(pageObjNum) + ".png"
  136. }
  137. if fileManager.fileExists(atPath: imageFilePath) {
  138. try? fileManager.removeItem(atPath: imageFilePath)
  139. }
  140. }
  141. }
  142. class KMNThumbnailManager: NSObject {
  143. static let manager = KMNThumbnailManager()
  144. var copyPages: [CPDFPage] = []
  145. var copyDocument: [CPDFDocument] = []
  146. class func filePathId(for filePath: String) -> String {
  147. return "\(filePath.hash)"
  148. }
  149. class func clearCacheFilePath(filePath:String) {
  150. let filePathID = KMNThumbnailManager.filePathId(for: filePath)
  151. let docsFilePath = kmnThumbnailFolder + "/" + filePathID
  152. let fileManager = FileManager.default
  153. if fileManager.fileExists(atPath: docsFilePath) {
  154. try? fileManager.removeItem(atPath: docsFilePath)
  155. }
  156. }
  157. class func clearCacheThumImage() {
  158. if FileManager.default.fileExists(atPath: kmnThumbnailFolder) {
  159. try?FileManager.default.removeItem(atPath: kmnThumbnailFolder)
  160. }
  161. }
  162. }