KMNThumbnailManager.swift 6.8 KB

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