-
Notifications
You must be signed in to change notification settings - Fork 1
/
NSImage+QuickLook.m
57 lines (47 loc) · 1.95 KB
/
NSImage+QuickLook.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//
// NSImage+QuickLook.m
// QuickLookTest
//
// Created by Matt Gemmell on 29/10/2007.
//
#import "NSImage+QuickLook.h"
#import <QuickLook/QuickLook.h> // Remember to import the QuickLook framework into your project!
@implementation NSImage (QuickLook)
+ (NSImage *)imageWithPreviewOfFileAtPath:(NSString *)path ofSize:(NSSize)size asIcon:(BOOL)icon
{
NSURL *fileURL = [NSURL fileURLWithPath:path];
if (!path || !fileURL) {
return nil;
}
NSDictionary *dict = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:icon]
forKey:(NSString *)kQLThumbnailOptionIconModeKey];
CGImageRef ref = QLThumbnailImageCreate(kCFAllocatorDefault,
(CFURLRef)fileURL,
CGSizeMake(size.width, size.height),
(CFDictionaryRef)dict);
if (ref != NULL) {
// Take advantage of NSBitmapImageRep's -initWithCGImage: initializer, new in Leopard,
// which is a lot more efficient than copying pixel data into a brand new NSImage.
// Thanks to Troy Stephens @ Apple for pointing this new method out to me.
NSBitmapImageRep *bitmapImageRep = [[NSBitmapImageRep alloc] initWithCGImage:ref];
NSImage *newImage = nil;
if (bitmapImageRep) {
newImage = [[NSImage alloc] initWithSize:[bitmapImageRep size]];
[newImage addRepresentation:bitmapImageRep];
[bitmapImageRep release];
if (newImage) {
return [newImage autorelease];
}
}
CFRelease(ref);
} else {
// If we couldn't get a Quick Look preview, fall back on the file's Finder icon.
NSImage *icon = [[NSWorkspace sharedWorkspace] iconForFile:path];
if (icon) {
[icon setSize:size];
}
return icon;
}
return nil;
}
@end