-
Notifications
You must be signed in to change notification settings - Fork 24
/
GenerateThumbnailForURL.m
76 lines (56 loc) · 2.1 KB
/
GenerateThumbnailForURL.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <CoreFoundation/CoreFoundation.h>
#include <CoreServices/CoreServices.h>
#include <QuickLook/QuickLook.h>
#include <WebKit/WebKit.h>
#include "quicklooknfo.h"
#define minSize 32
#define thumbnailWidth 490.0
#define thumbnailHeight 800.0
OSStatus
GenerateThumbnailForURL(void *thisInterface,
QLThumbnailRequestRef thumbnail,
CFURLRef url,
CFStringRef contentTypeUTI,
CFDictionaryRef options,
CGSize maxSize)
{
if (maxSize.width < minSize || maxSize.height < minSize)
return noErr;
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSRect renderRect = NSMakeRect(0.0, 0.0, thumbnailWidth, thumbnailHeight);
float scale = maxSize.height/thumbnailHeight;
NSSize scaleSize = NSMakeSize(scale, scale);
CGSize thumbSize = NSSizeToCGSize(
NSMakeSize((maxSize.width * (thumbnailWidth/thumbnailHeight)),
maxSize.height));
CFDataRef dataRef = createHTMLPreview( url );
NSData *data = (id)dataRef;
WebView* webView = [[WebView alloc] initWithFrame:renderRect];
[webView scaleUnitSquareToSize:scaleSize];
[[[webView mainFrame] frameView] setAllowsScrolling:NO];
[[webView mainFrame] loadData:data MIMEType:@"text/html"
textEncodingName:@"UTF-8" baseURL:nil];
while([webView isLoading]) {
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, true);
}
// Get a context to render into
CGContextRef context =
QLThumbnailRequestCreateContext(thumbnail, thumbSize, false, NULL);
if(context != NULL) {
NSGraphicsContext* nsContext =
[NSGraphicsContext
graphicsContextWithGraphicsPort:(void *)context
flipped:[webView isFlipped]];
[webView displayRectIgnoringOpacity:[webView bounds]
inContext:nsContext];
QLThumbnailRequestFlushContext(thumbnail, context);
CFRelease(context);
}
CFRelease(data);
[webView release];
[pool release];
return noErr;
}
void CancelThumbnailGeneration(void* thisInterface, QLThumbnailRequestRef thumbnail)
{
}