ProcessThumbnal.swift 6.6 KB

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