ProcessThumbnal.swift 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 retryCount = 0;
  51. var didCallback = false;
  52. let url = URL.init(fileURLWithPath: filePath, isDirectory: false)
  53. let request = QLThumbnailGenerator.Request.init(fileAt: url, size: outputSize, scale: 2.0, representationTypes: QLThumbnailGenerator.Request.RepresentationTypes.thumbnail)
  54. generate.generateBestRepresentation(for: request,
  55. completion: { (representation, error) in
  56. if (error != nil) {
  57. NSLog("\(error)");
  58. }
  59. if nil != representation {
  60. autoreleasepool {
  61. let image = representation!.nsImage as NSImage
  62. if nil != image {
  63. didFinished = true
  64. let rep = NSBitmapImageRep.init(cgImage: image.cgImage(forProposedRect: nil, context: nil, hints: nil)!)
  65. let data = rep.representation(using: NSBitmapImageRep.FileType.png, properties: [:]);
  66. let url = URL.init(fileURLWithPath: desPath, isDirectory: false)
  67. try? data!.write(to: url)
  68. }
  69. }
  70. }
  71. if (!didCallback && (didFinished || retryCount > 2)) {
  72. didCallback = true;
  73. complention(didFinished)
  74. generate.cancel(request)
  75. }else {
  76. retryCount += 1;
  77. }
  78. })
  79. }
  80. //同步方法
  81. class func process(_ filePath:String, desPath:String) -> Bool {
  82. let kDefaultThumbnailSize = CGSizeMake(1000.0, 1000.0)
  83. return process(filePath, desPath: desPath, outputSize: kDefaultThumbnailSize)
  84. }
  85. class func process(_ filePath:String, desPath:String, outputSize:CGSize) -> Bool {
  86. if NSArray(array: ["PDF", "pdf"]).contains(NSString(string: filePath).pathExtension) {
  87. return autoreleasepool {
  88. let status = FileConverter.shared().converter(filePath, inDesPath: desPath)
  89. return status == 1
  90. }
  91. }
  92. if NSArray(array: ["JPG", "jpg"]).contains(NSString(string: filePath).pathExtension) {
  93. return true
  94. }
  95. if NSArray(array: ["bmp", "BMP", "PNG", "png", "JPG", "jpg"]).contains(NSString(string: filePath).pathExtension) {
  96. return autoreleasepool {
  97. let imageSource = CGImageSourceCreateWithURL(URL.init(fileURLWithPath: filePath, isDirectory: false) as CFURL, nil)
  98. if nil == imageSource {
  99. return false
  100. }
  101. // Get the BMP image
  102. let cgimage = CGImageSourceCreateImageAtIndex(imageSource!, 0, nil)
  103. if nil == cgimage {
  104. return false
  105. }
  106. let rep = NSBitmapImageRep.init(cgImage: cgimage!)
  107. let data = rep.representation(using: NSBitmapImageRep.FileType.png, properties: [:]);
  108. let url = URL.init(fileURLWithPath: desPath, isDirectory: false)
  109. try? data!.write(to: url)
  110. return true
  111. }
  112. }
  113. var didFinished = false
  114. let semaphore = DispatchSemaphore(value: 0)
  115. let url = URL.init(fileURLWithPath: filePath, isDirectory: false)
  116. DispatchQueue.main.async {
  117. let request = QLThumbnailGenerator.Request.init(fileAt: url, size: outputSize, scale: 2.0, representationTypes: QLThumbnailGenerator.Request.RepresentationTypes.thumbnail)
  118. generate.generateBestRepresentation(for: request,
  119. completion: { (representation, error) in
  120. if nil != representation {
  121. autoreleasepool {
  122. let image = representation!.nsImage as NSImage
  123. if nil != image {
  124. didFinished = true
  125. let rep = NSBitmapImageRep.init(cgImage: image.cgImage(forProposedRect: nil, context: nil, hints: nil)!)
  126. let data = rep.representation(using: NSBitmapImageRep.FileType.png, properties: [:]);
  127. let url = URL.init(fileURLWithPath: desPath, isDirectory: false)
  128. try? data!.write(to: url)
  129. }
  130. }
  131. }
  132. generate.cancel(request)
  133. semaphore.signal()
  134. })
  135. }
  136. semaphore.wait()
  137. return didFinished
  138. }
  139. }