问题描述
我有一个NSMatrix与几个NSButtons在它没有文本,但只是图像。其中一个图像是从互联网上下载的,我想在我的OS X应用程序的圆角。
我发现一个答案,几乎是我在看为:但可悲的是它行为疯狂,当我使用it:
//在我的NSButtonCell子类
- (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),那么它是一个图像(PNG)。完全破坏,我只在黑色背景上看到几个白色像素。
如果图像不透明,那么它会得到圆角,但旋转180°,我不知道为什么。
有任何建议吗?
解决方案您需要确保在绘制图像之前正确设置图像的大小,并且应该使用 NSImage
方法 drawInRect:fromRect:operation:fraction:respectFlipped :hints:
以确保图像以正确的方式绘制:
- (void)drawImage :(NSImage *)imageFrame:(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 setSize:imageFrame.size];
//绘制图像
[image drawInRect:imageFrame fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0 respectFlipped:YES hints:nil];
[NSGraphicsContext restoreGraphicsState];
}
如果这样做,图像应该正确绘制,即使它是一个半透明的PNG图片。
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?
解决方案 You need to make sure you set the image's size correctly before drawing it, and you should use the NSImage
method drawInRect:fromRect:operation:fraction:respectFlipped:hints:
to ensure the image is drawn the correct way up:
- (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];
}
The image should draw correctly if you do this, even if it's a translucent PNG image.
这篇关于在NSButtonCell中的圆角NSImage角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!