KMNThumbnailManager.swift 5.3 KB

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