// // 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 } }