ProcessThumbnal.swift 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. let generator = QLThumbnailGenerator.shared
  12. class ProcessThumbnal : NSObject {
  13. class func process(_ filePath:String, desPath:String) -> Bool {
  14. let kDefaultThumbnailSize = CGSizeMake(1000.0, 1000.0)
  15. return process(filePath, desPath: desPath, outputSize: kDefaultThumbnailSize)
  16. }
  17. class func process(_ filePath:String, desPath:String, outputSize:CGSize) -> Bool {
  18. var didFinished = false
  19. let semaphore = DispatchSemaphore(value: 0)
  20. let url = URL.init(filePath: filePath)
  21. generator.generateRepresentations(for: QLThumbnailGenerator.Request.init(fileAt: url, size: CGSizeMake(1024.0, 10240.0), scale: 1.0, representationTypes: QLThumbnailGenerator.Request.RepresentationTypes.thumbnail),
  22. update: { (representation, type, error) in
  23. let image = representation!.nsImage as NSImage
  24. if nil != image {
  25. didFinished = true
  26. try? image.tiffRepresentation?.write(to: URL.init(filePath: desPath))
  27. }
  28. semaphore.signal()
  29. })
  30. semaphore.wait()
  31. return didFinished
  32. }
  33. }