プレビュー機能を実装する
GeneratePreviewForURLの役割
ここまでコーディング前の下準備をしてきましたが、準備も終わったのでここからいよいよコーディングを始めます。
まず GeneratePreviewForURL.mを眺めてみましょう。プロジェクトのテンプレートには以下のようなコードが書いてあります。
元のままだと画面からはみ出すので、適当に改行を入れてあります。
#include <CoreFoundation/CoreFoundation.h>
#include <CoreServices/CoreServices.h>
#include <QuickLook/QuickLook.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)
{
/*
warning
To complete your generator please implement the function
GeneratePreviewForURL in GeneratePreviewForURL.c
*/
return noErr;
}
void CancelPreviewGeneration(void* thisInterface, QLPreviewRequestRef preview)
{
// implement only if supported
}
これを見ると、GeneratePreviewForURL()でプレビュー画像を作って返せば良い事がわかります。画像を返す一つの方法はQLPreviewRequestSetDataRepresentation()を使う事です。
QL_EXPORT void QLPreviewRequestSetDataRepresentation(
QLPreviewRequest preview,
CFDataRef data,
CFStringRef contentTypeUTI,
CFDictionaryRef properties
);
引数のpreviewで、どのプレビューリクエストに対して画像を返すのかを指定します。dataは返すべきデータで、今回は画像データを渡します。contentTypeUTIでデータの種類を指定します。画像データの場合はkUTTypeImageです。propertiesは追加情報を入れる辞書です。
NSImageに直接描画して、そのデータを TIFFRepresentationで取り出し、QLPreviewRequestSetDataRepresentation()の引数として渡す事で、プレビュー画像を返す事ができます。やってみましょう。
OSStatus GeneratePreviewForURL(
void *thisInterface,
QLPreviewRequestRef preview,
CFURLRef url,
CFStringRef contentTypeUTI,
CFDictionaryRef options)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSBezierPath *bp;
NSSize imageSize = NSMakeSize(300,200);
NSImage *image = [[[NSImage alloc] initWithSize:imageSize] autorelease];
[image lockFocus];
[[NSColor whiteColor] set];
[NSBezierPath fillRect:NSMakeRect(0,0,300,200)];
bp = [NSBezierPath bezierPathWithOvalInRect:NSMakeRect(90,40,120,120)];
[[NSColor redColor] set];
[bp fill];
[image unlockFocus];
CFDataRef imageData = (CFDataRef)[image TIFFRepresentation];
QLPreviewRequestSetDataRepresentation(preview,imageData,kUTTypeImage,nil);
[pool release];
return noErr;
}
Cocoaフレームワークの関数やオブジェクトを使うためには#import <Cocoa/Cocoa.h>の一行を追加する必要がありますのでお忘れなく。
ビルドして実行すると、以下のウィンドウが表示されました。

ちなみに正式な日の丸は国旗のの縦:横が2:3で、日の丸の直径が縦の3/5だそうです。現代社会Quiz 政治制度・機関2
の202を参考にさせていただきました。
余談ですが、CGLayerのサンプルプログラムにアメリカの国旗を描くプログラムというものがあります。Example: Using Multiple CGLayer objects to Draw a Flag

HOME
前のページへ