获取文件的QuickLook预览图像

时间:2012-01-11 07:09:41

标签: objective-c swift macos quicklook

有没有办法获得文件的快速预览图像?

我正在寻找类似的东西:

NSImage *image = [QuickLookPreviewer quickLookPreviewForFile:path];

2 个答案:

答案 0 :(得分:5)

请参阅文档中的QLThumbnailRequesthttps://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);

答案 1 :(得分:1)

在Swift中,我最终得到了这样的内容(应该更换外包装):

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))