KMNThumbnailManager.swift 6.0 KB

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