Database Reference
In-Depth Information
Like the thumbnail generator in
Generating the Quick Look Thumbnail
, on
page 169
, the preview generator is contained within one function call, and we
are expected to populate the
QLPreviewRequestRef
and return
noErr
. Also, like the
thumbnail generator, we will always return
noErr
no matter what happens
within our function call.
Unlike the thumbnail generator, we are not going to be working with just the
image for the recipe. Instead, we will generate a full HTML page that contains
a large amount of information about the recipe and use that as our preview.
Although it would be possible to generate the entire HTML page in code, I am
rather lazy and would rather avoid that. Instead, let's take advantage of some
XPath queries to locate the correct nodes inside a template HTML file, change
the values to be appropriate for our current recipe, and use that to generate
the
QLPreviewRequestRef
.
NSString *bundleID = @
"com.pragprog.quicklook.grokkingrecipe"
;
OSStatus GeneratePreviewForURL(
void
*thisInterface,
QLPreviewRequestRef preview,
CFURLRef url,
CFStringRef contentTypeUTI,
CFDictionaryRef options)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
@try
{
NSDictionary *metadata;
metadata = [NSDictionary dictionaryWithContentsOfURL:(NSURL*)url];
if
(!metadata)
return
noErr;
NSLog(@
"metadata: %@"
, metadata);
NSString *imagePath = [metadata valueForKey:@
"kPPImagePath"
];
NSData *imageData = [[NSData alloc] initWithContentsOfFile:imagePath];
if
(!imageData)
return
noErr;
To start with, we load the metadata dictionary as we have previously. We are
also going to load the image data into an
NSData
object again. Assuming there
are no issues with either the metadata or the image loading, the next step is
to set up the options for the HTML page.
NSMutableDictionary *imageDict = [NSMutableDictionary dictionary];
[imageDict setValue:imageData
forKey:(id)kQLPreviewPropertyAttachmentDataKey];
if
(QLPreviewRequestIsCancelled(preview))
return
noErr;
NSMutableDictionary *attachments = [NSMutableDictionary dictionary];