ProcessThumbnal.swift 7.3 KB

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