简体   繁体   中英

Get QuickLook preview image for file

Is there any way to get the quick look preview image for a file?

I'm looking for something like this:

NSImage *image = [QuickLookPreviewer quickLookPreviewForFile:path];

See QLThumbnailRequest in the docs: https://developer.apple.com/library/mac/#documentation/UserExperience/Reference/QLThumbnailRequest_Ref/Reference/reference.html

NSURL *path = aFileUrl;

NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:(NSString *)kQLThumbnailOptionIconModeKey];

CGImageRef ref = QLThumbnailImageCreate(kCFAllocatorDefault, (CFURLRef)path, CGSizeMake(600, 800 /* Or whatever size you want */), (CFDictionaryRef)options);

In Swift I ended up with something like this (the force unwraps should be replaced):

let options = [
  kQLThumbnailOptionIconModeKey: false
]

let ref = QLThumbnailCreate(
  kCFAllocatorDefault,
  url as NSURL,
  CGSize(width: 150, height: 150),
  options as CFDictionary
)

let thumbnail = ref!.takeRetainedValue()
let cgImageRef = QLThumbnailCopyImage(thumbnail)
let cgImage = cgImageRef!.takeRetainedValue()
let image = NSImage(cgImage: cgImage, size: CGSize(width: cgImage.width, height: cgImage.height))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM