-
Notifications
You must be signed in to change notification settings - Fork 0
/
GeneratePreviewForURL.m
46 lines (39 loc) · 1.87 KB
/
GeneratePreviewForURL.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
#import <Foundation/Foundation.h>
#include <CoreFoundation/CoreFoundation.h>
#include <CoreServices/CoreServices.h>
#include <QuickLook/QuickLook.h>
#import "SpectrogramImage.h"
/* -----------------------------------------------------------------------------
Generate a preview for file
This function's job is to create preview for designated file
----------------------------------------------------------------------------- */
OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview, CFURLRef url, CFStringRef contentTypeUTI, CFDictionaryRef options)
{
// Preset values
NSUInteger maxWidth = 1800;
NSUInteger maxHeight = 512;
CGImageRef image = CreateImageForURL(url, maxWidth, maxHeight, YES);
if (image == NULL) {
return -1;
}
CGFloat width = CGImageGetWidth(image);
CGFloat height = CGImageGetHeight(image);
// NSLog(@"Got render size of %f x %f", width, height);
if (QLPreviewRequestIsCancelled(preview)) {
CGImageRelease(image);
return kQLReturnNoError;
}
@autoreleasepool {
NSDictionary *newOpt = [NSDictionary dictionaryWithObjectsAndKeys:(NSString *)[(__bridge NSDictionary *)options objectForKey:(NSString *)kQLPreviewPropertyDisplayNameKey], kQLPreviewPropertyDisplayNameKey, [NSNumber numberWithFloat:width], kQLPreviewPropertyWidthKey, [NSNumber numberWithFloat:height], kQLPreviewPropertyHeightKey, nil];
CGContextRef ctx = QLPreviewRequestCreateContext(preview, CGSizeMake(width, height), YES, (__bridge CFDictionaryRef)newOpt);
CGContextDrawImage(ctx, CGRectMake(0,0,width,height), image);
QLPreviewRequestFlushContext(preview, ctx);
CGImageRelease(image);
CGContextRelease(ctx);
}
return kQLReturnNoError;
}
void CancelPreviewGeneration(void *thisInterface, QLPreviewRequestRef preview)
{
// Implement only if supported
}