-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenerateThumbnailForURL.m
56 lines (43 loc) · 1.88 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
#import <Cocoa/Cocoa.h>
#include <CoreServices/CoreServices.h>
#include <QuickLook/QuickLook.h>
#import <WebKit/WebKit.h>
#import "QLMarkdown.h"
/* -----------------------------------------------------------------------------
Generate a thumbnail for file
This function's job is to create thumbnail for designated file as fast as possible
----------------------------------------------------------------------------- */
OSStatus GenerateThumbnailForURL(void *thisInterface, QLThumbnailRequestRef thumbnail, CFURLRef url, CFStringRef contentTypeUTI, CFDictionaryRef options, CGSize maxSize)
{
NSString *htmlString = ConvertURLToText((NSURL*)url);
NSRect viewRect = NSMakeRect(0.0, 0.0, 800.0, 600.0);
float scale = maxSize.height / 800.0;
// NSLog(@"scale: %f, %@", scale, NSStringFromSize(NSSizeFromCGSize(maxSize)));
NSSize scaleSize = NSMakeSize(scale, scale);
CGSize thumbSize = NSSizeToCGSize(NSMakeSize((maxSize.width * (600.0/800.0)),
maxSize.height));
WebView* webView = [[WebView alloc] initWithFrame: viewRect];
[webView scaleUnitSquareToSize: scaleSize];
[[[webView mainFrame] frameView] setAllowsScrolling:NO];
[[webView mainFrame] loadHTMLString:htmlString baseURL:nil];
while ([webView isLoading])
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.0, true);
[webView display];
CGContextRef context =
QLThumbnailRequestCreateContext(thumbnail, thumbSize, false, NULL);
if (context) {
NSGraphicsContext* nsContext =
[NSGraphicsContext
graphicsContextWithGraphicsPort: (void*) context
flipped: [webView isFlipped]];
[webView displayRectIgnoringOpacity: [webView bounds]
inContext: nsContext];
QLThumbnailRequestFlushContext(thumbnail, context);
CFRelease(context);
}
return noErr;
}
void CancelThumbnailGeneration(void* thisInterface, QLThumbnailRequestRef thumbnail)
{
// implement only if supported
}