1

テキストがなく画像のみのNSButtonがいくつかあるNSMatrixがあります。画像の1つはインターネットからダウンロードされており、OSXアプリケーションで角を丸めて表示したいと思います。

私が探していたものとほぼ同じ答えが1つ見つかりました。丸みを帯びたNSImageを描画する方法ですが、残念ながら、使用するとクレイジーに動作します。

// In my NSButtonCell subclass
- (void)drawImage:(NSImage*)image withFrame:(NSRect)imageFrame inView:(NSView*)controlView
{
    // [super drawImage:image withFrame:imageFrame inView:controlView];

    [NSGraphicsContext saveGraphicsState];

    NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:imageFrame xRadius:5 yRadius:5];
    [path addClip];

    [image drawInRect:imageFrame fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];

    [NSGraphicsContext restoreGraphicsState];
}

問題は、画像が部分的に透明(PNG)の場合、完全に破壊され、黒い背景に2、3個の白いピクセルしか表示されないことです。

また、画像が透明でない場合は、角が丸くなりますが、180°回転します。理由はわかりません。

助言がありますか?

4

1 に答える 1

3

画像を描画する前に、画像のサイズを正しく設定していることを確認する必要があります。また、このNSImage方法drawInRect:fromRect:operation:fraction:respectFlipped:hints:を使用して、画像が正しい方向に描画されていることを確認する必要があります。

- (void)drawImage:(NSImage*)image withFrame:(NSRect)imageFrame inView:(NSView*)controlView
{
    // [super drawImage:image withFrame:imageFrame inView:controlView];

    [NSGraphicsContext saveGraphicsState];

    NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:imageFrame xRadius:5 yRadius:5];
    [path addClip];

    //set the size
    [image setSize:imageFrame.size];

    //draw the image
    [image drawInRect:imageFrame fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0 respectFlipped:YES hints:nil];

    [NSGraphicsContext restoreGraphicsState];
}

これを行うと、半透明のPNG画像であっても、画像は正しく描画されます。

于 2011-11-30T23:38:19.007 に答える