-
Notifications
You must be signed in to change notification settings - Fork 142
/
GenerateThumbnailForURL.m
59 lines (46 loc) · 2.59 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
#import <Cocoa/Cocoa.h>
#import <CoreFoundation/CoreFoundation.h>
#import <CoreServices/CoreServices.h>
#import <Foundation/Foundation.h>
#import <QuickLook/QuickLook.h>
#import <WebKit/WebKit.h>
OSStatus GenerateThumbnailForURL(void *thisInterface, QLThumbnailRequestRef thumbnail, CFURLRef url,
CFStringRef contentTypeUTI, CFDictionaryRef options, CGSize maxSize);
void CancelThumbnailGeneration(void *thisInterface, QLThumbnailRequestRef thumbnail);
/* -----------------------------------------------------------------------------
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 *content = [NSString stringWithContentsOfURL:(__bridge NSURL *)url encoding:NSUTF8StringEncoding error:nil];
if (content) {
// Encapsulate the content of the .strings file in HTML
NSData *data = [content dataUsingEncoding:NSUTF8StringEncoding];
NSRect _rect = NSMakeRect(0.0, 0.0, 600.0, 800.0);
float _scale = maxSize.height / 800.0;
NSSize _scaleSize = NSMakeSize(_scale, _scale);
CGSize _thumbSize = NSSizeToCGSize(NSMakeSize((maxSize.width * (600.0 / 800.0)), maxSize.height));
// Create the webview to display the thumbnail
WebView *_webView = [[WebView alloc] initWithFrame:_rect];
[_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);
[_webView display];
// Draw the webview in the correct context
CGContextRef _context = QLThumbnailRequestCreateContext(thumbnail, _thumbSize, false, NULL);
if (_context) {
NSGraphicsContext *_graphicsContext = [NSGraphicsContext graphicsContextWithGraphicsPort:(void *)_context
flipped:_webView.isFlipped];
[_webView displayRectIgnoringOpacity:_webView.bounds inContext:_graphicsContext];
QLThumbnailRequestFlushContext(thumbnail, _context);
CFRelease(_context);
}
}
return noErr;
}
void CancelThumbnailGeneration(void *thisInterface, QLThumbnailRequestRef thumbnail) {
// implement only if supported
}