123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- //
- // ProcessThumbnal.swift
- // ProcessCheckFile
- //
- // Created by 朱东勇 on 2022/12/9.
- //
- import Foundation
- import QuickLook
- import QuickLookUI
- import QuickLookThumbnailing
- import ImageIO
- let generate = QLThumbnailGenerator.shared
- let queue = OperationQueue()
- class ProcessThumbnal : NSObject {
-
- // 异步方法
- class func process(_ filePath:String, desPath:String, complention:@escaping (_ success:Bool) -> ()) {
- let kDefaultThumbnailSize = CGSizeMake(1000.0, 1000.0)
- return process(filePath, desPath: desPath, outputSize: kDefaultThumbnailSize, complention: complention)
- }
-
- class func process(_ filePath:String, desPath:String, outputSize:CGSize, complention:@escaping (_ success:Bool) -> ()) {
- if NSArray(array: ["PDF", "pdf"]).contains(NSString(string: filePath).pathExtension) {
- queue.maxConcurrentOperationCount = 1;
- queue.addOperation {
- autoreleasepool {
- let status = FileConverter.shared().converter(filePath, inDesPath: desPath)
- complention(status == 1)
- }
- }
-
- return
- }
-
- if NSArray(array: ["JPG", "jpg"]).contains(NSString(string: filePath).pathExtension) {
- complention(true)
- return
- }
-
- if NSArray(array: ["bmp", "BMP", "PNG", "png", "JPG", "jpg"]).contains(NSString(string: filePath).pathExtension) {
- complention(autoreleasepool {
- let imageSource = CGImageSourceCreateWithURL(URL.init(fileURLWithPath: filePath, isDirectory: false) as CFURL, nil)
-
- if nil == imageSource {
- return false
- }
-
- // Get the BMP image
- let cgimage = CGImageSourceCreateImageAtIndex(imageSource!, 0, nil)
- if nil == cgimage {
- return false
- }
-
- let rep = NSBitmapImageRep.init(cgImage: cgimage!)
- let data = rep.representation(using: NSBitmapImageRep.FileType.png, properties: [:]);
-
- let url = URL.init(fileURLWithPath: desPath, isDirectory: false)
- try? data!.write(to: url)
-
- return true
- })
-
- return
- }
-
- var didFinished = false
- // var retryCount = 0;
- var didCallback = false;
-
- let url = URL.init(fileURLWithPath: filePath, isDirectory: false)
- let request = QLThumbnailGenerator.Request.init(fileAt: url, size: outputSize, scale: 2.0, representationTypes: QLThumbnailGenerator.Request.RepresentationTypes.thumbnail)
- generate.generateBestRepresentation(for: request,
- completion: { (representation, error) in
- if (error != nil) {
- NSLog("KdanAuto:\(error)");
- }
- if nil != representation {
- autoreleasepool {
- let image = representation!.nsImage as NSImage
-
- if nil != image {
- didFinished = true
-
- let rep = NSBitmapImageRep.init(cgImage: image.cgImage(forProposedRect: nil, context: nil, hints: nil)!)
- let data = rep.representation(using: NSBitmapImageRep.FileType.png, properties: [:]);
-
- let url = URL.init(fileURLWithPath: desPath, isDirectory: false)
- try? data!.write(to: url)
- }
- }
- }
-
- if !didCallback
- // && (didFinished || retryCount >= 1)
- {
- generate.cancel(request)
-
- didCallback = true;
- complention(didFinished)
-
- // }else {
- // retryCount += 1;
- }
- })
- }
-
- //同步方法
- class func process(_ filePath:String, desPath:String) -> Bool {
- let kDefaultThumbnailSize = CGSizeMake(1000.0, 1000.0)
-
- return process(filePath, desPath: desPath, outputSize: kDefaultThumbnailSize)
- }
-
- class func process(_ filePath:String, desPath:String, outputSize:CGSize) -> Bool {
- if NSArray(array: ["PDF", "pdf"]).contains(NSString(string: filePath).pathExtension) {
- return autoreleasepool {
- let status = FileConverter.shared().converter(filePath, inDesPath: desPath)
- return status == 1
- }
- }
-
- if NSArray(array: ["JPG", "jpg"]).contains(NSString(string: filePath).pathExtension) {
- return true
- }
-
- if NSArray(array: ["bmp", "BMP", "PNG", "png", "JPG", "jpg"]).contains(NSString(string: filePath).pathExtension) {
- return autoreleasepool {
- let imageSource = CGImageSourceCreateWithURL(URL.init(fileURLWithPath: filePath, isDirectory: false) as CFURL, nil)
-
- if nil == imageSource {
- return false
- }
-
- // Get the BMP image
- let cgimage = CGImageSourceCreateImageAtIndex(imageSource!, 0, nil)
- if nil == cgimage {
- return false
- }
-
- let rep = NSBitmapImageRep.init(cgImage: cgimage!)
- let data = rep.representation(using: NSBitmapImageRep.FileType.png, properties: [:]);
-
- let url = URL.init(fileURLWithPath: desPath, isDirectory: false)
- try? data!.write(to: url)
-
- return true
- }
- }
-
- var didFinished = false
-
- let semaphore = DispatchSemaphore(value: 0)
- let url = URL.init(fileURLWithPath: filePath, isDirectory: false)
- DispatchQueue.main.async {
- let request = QLThumbnailGenerator.Request.init(fileAt: url, size: outputSize, scale: 2.0, representationTypes: QLThumbnailGenerator.Request.RepresentationTypes.thumbnail)
- generate.generateBestRepresentation(for: request,
- completion: { (representation, error) in
- autoreleasepool {
- if nil != representation {
- autoreleasepool {
- let image = representation!.nsImage as NSImage
-
- if nil != image {
- didFinished = true
-
- let rep = NSBitmapImageRep.init(cgImage: image.cgImage(forProposedRect: nil, context: nil, hints: nil)!)
- let data = rep.representation(using: NSBitmapImageRep.FileType.png, properties: [:]);
-
- let url = URL.init(fileURLWithPath: desPath, isDirectory: false)
- try? data!.write(to: url)
- }
- }
- }
-
- generate.cancel(request)
- semaphore.signal()
- }
- })
- }
-
- semaphore.wait()
-
- return didFinished
- }
-
- }
|