KMNThumbnailManager.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. if (page?.rotation ?? 0) % 180 == 0 {
  29. pageSize = size
  30. } else {
  31. pageSize = CGSize(width: size.height, height: size.width)
  32. }
  33. thumbnaiPage = page
  34. pageRotate = 0
  35. isHoveBookMark = document.bookmark(forPageIndex: UInt(pageIndex)) != nil
  36. super.init()
  37. }
  38. public override init() {
  39. super.init()
  40. }
  41. func generateThumImage(completion: KMNThumbnailCompletion?) {
  42. let filePathID = fileId(for: thumbnailDocument)
  43. let docsFilePath = kmnThumbnailFolder + "/" + filePathID
  44. let fileManager = FileManager.default
  45. if !fileManager.fileExists(atPath: docsFilePath) {
  46. try? FileManager.default.createDirectory(atPath: docsFilePath, withIntermediateDirectories: true, attributes: nil)
  47. }
  48. let imageFilePath = docsFilePath + "/" + (self.thumbnaiPage?.label ?? String(self.pageIndex)) + ".png"
  49. if fileManager.fileExists(atPath: imageFilePath) {
  50. let image = NSImage.init(contentsOfFile: imageFilePath) ?? NSImage()
  51. if completion != nil {
  52. completion!(image)
  53. }
  54. } else {
  55. var thumSize = pageSize
  56. if pageSize.width < 100 {
  57. thumSize.width = 100
  58. thumSize.height *= 100/pageSize.width
  59. }
  60. thumbnaiPage?.thumbnail(of: thumSize, needReset: true,completion: { pageImage in
  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. if completion != nil {
  72. completion!(pageImage)
  73. }
  74. });
  75. }
  76. }
  77. fileprivate func fileId(for document: CPDFDocument) -> String {
  78. return "\(document.documentURL.path.hash)"
  79. }
  80. }
  81. class KMNThumbnailManager: NSObject {
  82. class func clearCacheThumImage() {
  83. if FileManager.default.fileExists(atPath: kmnThumbnailFolder) {
  84. try?FileManager.default.removeItem(atPath: kmnThumbnailFolder)
  85. }
  86. }
  87. }