グラフィックスコンテキストにイメージを描画する
描く場所を提供してもらう
データを作って渡すのではなく、「ここに描いて下さい」と指定された場所にイメージを描画していく方法もあります。
QLPreviewRequestCreateContext()で、描画する場所であるグラフィクスコンテキストを入手する事ができます。
QL_EXPORT CGContextRef QLPreviewRequestCreateContext(
QLPreviewRequestRef preview,
CGSize size,
Boolean isBitmap,
CFDictionaryRef properties
);
第一引数のpreviewで、どのプレビューリクエストにグラフィクスコンテキストを作ってもらうのかを指定します。sizeはプレビュー画像のサイズです。isBitmapをtrueにするとビットマップ用、falseにするとベクトル描画用のコンテキストになります。最後のpropertiesは追加情報用の辞書です。
グラフィックスコンテキストにおける文書イメージの描画
を参考にしてコードを書いてみると以下の様になります。
OSStatus GeneratePreviewForURL(
void *thisInterface,
QLPreviewRequestRef preview,
CFURLRef url,
CFStringRef contentTypeUTI,
CFDictionaryRef options)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSBezierPath *bp;
CGSize imageSize = CGSizeMake(300,200);
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:300],kQLPreviewPropertyWidthKey,
[NSNumber numberWithInt:200],kQLPreviewPropertyHeightKey,
nil];
CGContextRef cgContext = QLPreviewRequestCreateContext(
preview,imageSize,false,(CFDictionaryRef)dict);
if(cgContext)
{
NSGraphicsContext* context =
[NSGraphicsContext
graphicsContextWithGraphicsPort:(void *)cgContext
flipped:YES];
if(context)
{
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:context];
[[NSColor whiteColor] set];
[NSBezierPath fillRect:NSMakeRect(0,0,300,200)];
bp = [NSBezierPath bezierPathWithOvalInRect:
NSMakeRect(90,40,120,120)];
[[NSColor redColor] set];
[bp fill];
[NSGraphicsContext restoreGraphicsState];
}
QLPreviewRequestFlushContext(preview,cgContext);
CFRelease(cgContext);
}
[pool release];
return noErr;
}
ビルドして実行すると、以下のウィンドウが表示されました。前と同じに見えるかもしれませんが、QLPreviewRequestCreateContext()の第三引数をfalseにしているので、ベクトルグラフィクスになっています。
ウィンドウサイズを変えて拡大しても、ギザギザにならなくなりました。


HOME
前のページへ