xcatsan師匠がSimpleCapを作る過程で山ほどこの手の処理を書いていてくれているので、あれこれ参考にしながら作業をしてみる。
まずはNSImageViewにデスクトップのキャプチャ画像を表示させてみた。コントローラのawakeFormNibでviewを生成する。
@implementation MyController
-(void)awakeFromNib{
NSRect viewFrame = [[window contentView] bounds];
MyImageView *view=[[MyImageView alloc] initWithFrame:viewFrame];
[[window contentView] addSubview:view];
[view release];
}
@end
これだけならInterfaceBuilderで生成してもいいわけだけど、ま、あとあといろいろ変更してみようと考えている。Viewはカスタムクラスにして、こんな感じ。
@implementation MyImageView
-(id)initWithFrame:(NSRect)frameRect{
self = [super initWithFrame:frameRect];
if (self) {
[self setCaptureImage];
}
return self;
}
-(void)setCaptureImage{
CGWindowID window_id = kCGNullWindowID;
CGImageRef cgimage = CGWindowListCreateImage(CGRectInfinite, kCGWindowListOptionOnScreenBelowWindow,window_id, kCGWindowImageDefault);
NSBitmapImageRep *bitmap_rep = [[NSBitmapImageRep alloc] initWithCGImage:cgimage];
NSImage *image = [[[NSImage alloc] init] autorelease];
[image addRepresentation:bitmap_rep];
[bitmap_rep release];
[self setImage:image];
CGImageRelease(cgimage);
[self setImageScaling:NSImageScaleAxesIndependently];
NSLog(@"%d",[self imageScaling]);
}
setCaptureImage内の処理はほぼxcatsan師匠のコードの引き写し。これを自分で書けるようになるのが当面の目標だな。これでデスクトップ全体のキャプチャ画像がViewいっぱいに縮小されて表示できる。
NSImageViewの場合、drawRect:をオーバーライトしたら自動でimageを表示してくれなくなることが本日わかった。(^^;)もう、素人なんだから仕方ないわな。

0 件のコメント:
コメントを投稿