質問

私は、テキストがないが画像のみであるNSButtonsがいくつかあるNSMATRIXを持っています。画像の1つはインターネットからダウンロードされており、OS Xアプリケーションでコーナーを丸くしたいと思います。

私は私が探していたものでほぼ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)である場合、完全に破壊され、黒い背景に白いピクセルが数枚しか表示されないことです。

画像が透明でない場合は、丸い角を取得しますが、180°回転しており、その理由はわかりません。

助言がありますか?

役に立ちましたか?

解決

画像を描画する前に、画像のサイズを正しく設定することを確認する必要があります。 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画像であっても、画像は正しく描画する必要があります。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top