先日の SnapWeb に刺激され(?)ウィンドウキャプチャ方式のアプローチを試してみる (*)。
(*) 実際に SnapWebがこの方法をとっているかは不明
サンプルに「Capture」ボタンをつけて、AppController#capture: へ接続する。実装はこんな感じ。
{
 // (1) setup filename
 NSArray *paths = NSSearchPathForDirectoriesInDomains(
  NSDesktopDirectory, NSUserDomainMask, YES);
 NSString* path = [NSString stringWithFormat:@"%@/test.png",
  [paths objectAtIndex:0], nil];
 // (2) capture
 CGWindowID *windowIDs = calloc(1, sizeof(CGWindowID));
 windowIDs[0] = [_window windowNumber];
 CFArrayRef windowIDsArray =
  CFArrayCreate(kCFAllocatorDefault, (const void**)windowIDs, 1, NULL);
 CGImageRef cgimage =
  CGWindowListCreateImageFromArray(
   CGRectNull, windowIDsArray, kCGWindowImageBoundsIgnoreFraming);
 
 NSBitmapImageRep *bitmap_rep =
  [[[NSBitmapImageRep alloc] initWithCGImage:cgimage] autorelease];
 NSData* data = [bitmap_rep representationUsingType:NSPNGFileType
           properties:[NSDictionary dictionary]];
 [data writeToFile:path atomically:YES];
 CGImageRelease(cgimage);
 free(windowIDs);
 CFRelease(windowIDsArray);
 
}キャプチャの方法は昨年来紹介しているので必要なら過去のブログを参照してみて欲しい。私の場合、SimpleCapのコードからコピー&ペーストで5分ぐらいでできた(手抜き)。
さあ実行してみよう。Flashを含むページを開き「Capture」ボタンを押す。

出た。

ウィンドウを大きくすれば(当たり前だが)より大きな範囲をキャプチャできる。

理論上は、ウィンドウを Webサイトが表示できる十分な大きさまで広げてキャプチャすれば、Webサイト表示全体の画像が作れるわけだ。
サンプル:
WebKitSample-2.zipお膳立てはできた。
(続く)