KMNThumbnailManager.swift 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. var imageFilePath = ""
  49. if(pageObjNum == -1) {
  50. imageFilePath = ""
  51. } else {
  52. imageFilePath = docsFilePath + "/" + String(pageObjNum) + ".png"
  53. }
  54. var thumSize = pageSize
  55. if(imageFilePath.isEmpty == true) {
  56. let maxOrg = max(thumSize.width, thumSize.height)
  57. let minOrg = min(thumSize.width, thumSize.height)
  58. let maxWidth = 300.0
  59. let maxHeight = 500.0
  60. let scanW = maxWidth / minOrg
  61. let scanH = maxHeight / maxOrg
  62. let minScanl = min(scanW, scanH)
  63. if (thumbnaiPage?.rotation ?? 0) % 180 != 0 {
  64. thumSize.width = pageSize.height * minScanl
  65. thumSize.height = pageSize.width * minScanl
  66. } else {
  67. thumSize.width = pageSize.width * minScanl
  68. thumSize.height = pageSize.height * minScanl
  69. }
  70. thumbnaiPage?.thumbnail(of: thumSize, needReset: true,completion: { pageImage in
  71. if completion != nil {
  72. completion!(pageImage)
  73. }
  74. // 将 NSImage 转换为 PNG 数据
  75. guard let tiffData = pageImage?.tiffRepresentation,
  76. let bitmapImage = NSBitmapImageRep(data: tiffData),
  77. let pngData = bitmapImage.representation(using: .png, properties: [:]) else {
  78. return
  79. }
  80. do {
  81. try pngData.write(to: URL(fileURLWithPath: imageFilePath))
  82. } catch {
  83. }
  84. });
  85. } else {
  86. if fileManager.fileExists(atPath: imageFilePath) {
  87. let image = NSImage.init(contentsOfFile: imageFilePath) ?? NSImage()
  88. if completion != nil {
  89. completion!(image)
  90. }
  91. } else {
  92. let maxOrg = max(thumSize.width, thumSize.height)
  93. let minOrg = min(thumSize.width, thumSize.height)
  94. let maxWidth = 300.0
  95. let maxHeight = 500.0
  96. let scanW = maxWidth / minOrg
  97. let scanH = maxHeight / maxOrg
  98. let minScanl = min(scanW, scanH)
  99. if (thumbnaiPage?.rotation ?? 0) % 180 != 0 {
  100. thumSize.width = pageSize.height * minScanl
  101. thumSize.height = pageSize.width * minScanl
  102. } else {
  103. thumSize.width = pageSize.width * minScanl
  104. thumSize.height = pageSize.height * minScanl
  105. }
  106. thumbnaiPage?.thumbnail(of: thumSize, needReset: true,completion: { pageImage in
  107. if completion != nil {
  108. completion!(pageImage)
  109. }
  110. // 将 NSImage 转换为 PNG 数据
  111. guard let tiffData = pageImage?.tiffRepresentation,
  112. let bitmapImage = NSBitmapImageRep(data: tiffData),
  113. let pngData = bitmapImage.representation(using: .png, properties: [:]) else {
  114. return
  115. }
  116. do {
  117. try pngData.write(to: URL(fileURLWithPath: imageFilePath))
  118. } catch {
  119. }
  120. });
  121. }
  122. }
  123. }
  124. func removeCacheImage() {
  125. let filePathID = KMNThumbnailManager.filePathId(for: thumbnailDocument.documentURL.path)
  126. let docsFilePath = kmnThumbnailFolder + "/" + filePathID
  127. let fileManager = FileManager.default
  128. let pageObjNum = thumbnaiPage?.getObjNum() ?? -1
  129. var imageFilePath = ""
  130. if(pageObjNum == -1) {
  131. imageFilePath = ""
  132. } else {
  133. imageFilePath = docsFilePath + "/" + String(pageObjNum) + ".png"
  134. }
  135. if fileManager.fileExists(atPath: imageFilePath) {
  136. try? fileManager.removeItem(atPath: imageFilePath)
  137. }
  138. }
  139. }
  140. class KMNThumbnailManager: NSObject {
  141. static let manager = KMNThumbnailManager()
  142. var copyPages: [CPDFPage] = []
  143. var copyDocument: [CPDFDocument] = []
  144. class func filePathId(for filePath: String) -> String {
  145. return "\(filePath.hash)"
  146. }
  147. class func clearCacheFilePath(filePath:String) {
  148. let filePathID = KMNThumbnailManager.filePathId(for: filePath)
  149. let docsFilePath = kmnThumbnailFolder + "/" + filePathID
  150. let fileManager = FileManager.default
  151. if fileManager.fileExists(atPath: docsFilePath) {
  152. try? fileManager.removeItem(atPath: docsFilePath)
  153. }
  154. }
  155. class func clearCacheThumImage() {
  156. if FileManager.default.fileExists(atPath: kmnThumbnailFolder) {
  157. try?FileManager.default.removeItem(atPath: kmnThumbnailFolder)
  158. }
  159. }
  160. }