问题描述
我正在为我的应用程序编写 QuickLook 扩展程序,我希望能够显示特定文件的实际图标图像,如 Finder 中所示。我已经尝试为此使用 QLThumbnailGenerator,但它总是会返回一个纯白色的文档图标。
QLThumbnailGenerationRequest *request = [[QLThumbnailGenerationRequest alloc] initWithFileAtURL:url size:size scale:scale representationTypes:QLThumbnailGenerationRequestRepresentationTypeIcon];
QLThumbnailGenerator *generator = [QLThumbnailGenerator sharedGenerator];
__unsafe_unretained PreviewViewController *weakSelf = self;
[generator generateBestRepresentationForRequest:request completionHandler:^(QLThumbnailRepresentation *thumbnail,NSError *error) {
dispatch_async(dispatch_get_main_queue(),^{
if (thumbnail == nil || error) {
NSLog(@"Failed to generate thumbnail! %@",error);
// Handle the error case gracefully
} else {
// display the thumbnail that you created
NSLog(@"Generated thumbnail!");
weakSelf.imageView.image = thumbnail.NSImage;
}
});
}];
解决方法
这更像是一种解决方法,但改用 iconForFile 方法,即 _imageView.image = [[NSWorkspace sharedWorkspace] iconForFile:url.path]
做我想要的。