ProcessThumbnal.swift 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //
  2. // ProcessThumbnal.swift
  3. // ProcessCheckFile
  4. //
  5. // Created by 朱东勇 on 2022/12/9.
  6. //
  7. import Foundation
  8. import QuickLook
  9. import QuickLookUI
  10. import QuickLookThumbnailing
  11. import ImageIO
  12. let generate = QLThumbnailGenerator.shared
  13. class ProcessThumbnal : NSObject {
  14. // 异步方法
  15. class func process(_ filePath:String, desPath:String, complention:@escaping (_ success:Bool) -> ()) {
  16. let kDefaultThumbnailSize = CGSizeMake(1000.0, 1000.0)
  17. return process(filePath, desPath: desPath, outputSize: kDefaultThumbnailSize, complention: complention)
  18. }
  19. class func process(_ filePath:String, desPath:String, outputSize:CGSize, complention:@escaping (_ success:Bool) -> ()) {
  20. if NSArray(array: ["PDF", "pdf"]).contains(NSString(string: filePath).pathExtension) {
  21. return autoreleasepool {
  22. let status = FileConverter.shared().converter(filePath, inDesPath: desPath)
  23. complention(status == 1)
  24. }
  25. }
  26. if NSArray(array: ["JPG", "jpg"]).contains(NSString(string: filePath).pathExtension) {
  27. complention(true)
  28. return
  29. }
  30. if NSArray(array: ["bmp", "BMP", "PNG", "png", "JPG", "jpg"]).contains(NSString(string: filePath).pathExtension) {
  31. complention(autoreleasepool {
  32. let imageSource = CGImageSourceCreateWithURL(URL.init(fileURLWithPath: filePath, isDirectory: false) as CFURL, nil)
  33. if nil == imageSource {
  34. return false
  35. }
  36. // Get the BMP image
  37. let cgimage = CGImageSourceCreateImageAtIndex(imageSource!, 0, nil)
  38. if nil == cgimage {
  39. return false
  40. }
  41. let rep = NSBitmapImageRep.init(cgImage: cgimage!)
  42. let data = rep.representation(using: NSBitmapImageRep.FileType.png, properties: [:]);
  43. let url = URL.init(fileURLWithPath: desPath, isDirectory: false)
  44. try? data!.write(to: url)
  45. return true
  46. })
  47. return
  48. }
  49. var didFinished = false
  50. var didCallback = false;
  51. let url = URL.init(fileURLWithPath: filePath, isDirectory: false)
  52. let request = QLThumbnailGenerator.Request.init(fileAt: url, size: outputSize, scale: 2.0, representationTypes: QLThumbnailGenerator.Request.RepresentationTypes.thumbnail)
  53. generate.generateBestRepresentation(for: request,
  54. completion: { (representation, error) in
  55. if nil != representation {
  56. autoreleasepool {
  57. let image = representation!.nsImage as NSImage
  58. if nil != image {
  59. didFinished = true
  60. let rep = NSBitmapImageRep.init(cgImage: image.cgImage(forProposedRect: nil, context: nil, hints: nil)!)
  61. let data = rep.representation(using: NSBitmapImageRep.FileType.png, properties: [:]);
  62. let url = URL.init(fileURLWithPath: desPath, isDirectory: false)
  63. try? data!.write(to: url)
  64. }
  65. }
  66. }
  67. if (!didCallback) {
  68. didCallback = true;
  69. complention(didFinished)
  70. }
  71. generate.cancel(request)
  72. })
  73. }
  74. //同步方法
  75. class func process(_ filePath:String, desPath:String) -> Bool {
  76. let kDefaultThumbnailSize = CGSizeMake(1000.0, 1000.0)
  77. return process(filePath, desPath: desPath, outputSize: kDefaultThumbnailSize)
  78. }
  79. class func process(_ filePath:String, desPath:String, outputSize:CGSize) -> Bool {
  80. if NSArray(array: ["PDF", "pdf"]).contains(NSString(string: filePath).pathExtension) {
  81. return autoreleasepool {
  82. let status = FileConverter.shared().converter(filePath, inDesPath: desPath)
  83. return status == 1
  84. }
  85. }
  86. if NSArray(array: ["JPG", "jpg"]).contains(NSString(string: filePath).pathExtension) {
  87. return true
  88. }
  89. if NSArray(array: ["bmp", "BMP", "PNG", "png", "JPG", "jpg"]).contains(NSString(string: filePath).pathExtension) {
  90. return autoreleasepool {
  91. let imageSource = CGImageSourceCreateWithURL(URL.init(fileURLWithPath: filePath, isDirectory: false) as CFURL, nil)
  92. if nil == imageSource {
  93. return false
  94. }
  95. // Get the BMP image
  96. let cgimage = CGImageSourceCreateImageAtIndex(imageSource!, 0, nil)
  97. if nil == cgimage {
  98. return false
  99. }
  100. let rep = NSBitmapImageRep.init(cgImage: cgimage!)
  101. let data = rep.representation(using: NSBitmapImageRep.FileType.png, properties: [:]);
  102. let url = URL.init(fileURLWithPath: desPath, isDirectory: false)
  103. try? data!.write(to: url)
  104. return true
  105. }
  106. }
  107. var didFinished = false
  108. let semaphore = DispatchSemaphore(value: 0)
  109. let url = URL.init(fileURLWithPath: filePath, isDirectory: false)
  110. DispatchQueue.main.async {
  111. let request = QLThumbnailGenerator.Request.init(fileAt: url, size: outputSize, scale: 2.0, representationTypes: QLThumbnailGenerator.Request.RepresentationTypes.thumbnail)
  112. generate.generateBestRepresentation(for: request,
  113. completion: { (representation, error) in
  114. if nil != representation {
  115. autoreleasepool {
  116. let image = representation!.nsImage as NSImage
  117. if nil != image {
  118. didFinished = true
  119. let rep = NSBitmapImageRep.init(cgImage: image.cgImage(forProposedRect: nil, context: nil, hints: nil)!)
  120. let data = rep.representation(using: NSBitmapImageRep.FileType.png, properties: [:]);
  121. let url = URL.init(fileURLWithPath: desPath, isDirectory: false)
  122. try? data!.write(to: url)
  123. }
  124. }
  125. }
  126. generate.cancel(request)
  127. semaphore.signal()
  128. })
  129. }
  130. semaphore.wait()
  131. return didFinished
  132. }
  133. }