NSButtonCell 中的 NSImage 圆角
我有一个 NSMatrix,其中有几个 NSButton,它们没有文本,只有图像。其中一张图像是从互联网上下载的,我希望在我的 OS X 应用程序中将其变成圆角。
我找到了一个几乎正是我正在寻找的答案: 如何绘制圆形 NSImage< /a> 但遗憾的是,当我使用它时,它表现得很疯狂:
// 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°,我不知道为什么。
有什么建议吗?
I have a NSMatrix with a couple of NSButtons in it which have no Text but are only images. One of the images is downloaded from the internet and I would like to have it rounded corners in my OS X application.
I found one answer which nearly is what I was looking for: How to draw a rounded NSImage but sadly it acts crazy when I use it:
// 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];
}
The problem is that if a image is partly transparent (PNG) then it is completly destroyed and I only see a couple of white pixels on a black background.
And if the image is not transparent then it gets the rounded corners but is rotated 180° and I don't know why.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要确保在绘制图像之前正确设置图像的大小,并且应该使用
NSImage
方法drawInRect:fromRect:operation:fraction:respectFlipped:hints:
来确保图像以正确的方式向上绘制:如果执行此操作,即使它是半透明的 PNG 图像,图像也应该正确绘制。
You need to make sure you set the image's size correctly before drawing it, and you should use the
NSImage
methoddrawInRect:fromRect:operation:fraction:respectFlipped:hints:
to ensure the image is drawn the correct way up:The image should draw correctly if you do this, even if it's a translucent PNG image.