ProcessThumbnal.swift 7.1 KB

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