2

次のCSSコードで得られる効果を複製したいと思います。

background: white url(./img/background.png) no-repeat;

NSViewのサブクラスを作成し、次のdrawRectようにオーバーライドしました。

- (void)drawRect:(NSRect)dirtyRect
{
    dirtyRect = [self bounds];

    [[NSColor whiteColor] setFill];
    NSRectFill(dirtyRect);

    [[NSColor colorWithPatternImage:[NSImage imageNamed:@"background.png"]] setFill];
    NSRectFill(dirtyRect);
}

(英語が下手であることをお詫びします)

4

2 に答える 2

4

NSImage クラスリファレンスを見てください。画像は、drawInRect:fromRect:operation:fraction:を使用して描画できますまた、drawAtPoint:fromRect:operation:fraction:を使用して描画することもできます。

だからあなたはこれを使うことができます:

[[NSImage imageNamed:@"background.png"] drawInRect:dirtyRect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1]; // Passing NSZeroRect causes the entire image to draw. 

代わりにこれ:

[[NSColor colorWithPatternImage:[NSImage imageNamed:@"background.png"]] setFill];
    NSRectFill(dirtyRect);
于 2012-06-27T18:16:02.557 に答える
0

NSImage drawInRect:fromRect:operation:fraction:長方形をパターンカラーで塗りつぶす代わりに、ビューに画像を描画するために使用するだけです。

于 2012-06-27T18:15:52.697 に答える