gpt4 book ai didi

cocoa - 如何绘制圆形 NSImage

转载 作者:行者123 更新时间:2023-12-03 16:06:49 29 4
gpt4 key购买 nike

我正在尝试在 NSTableView 内创建带有圆角的 NSImageNSImageCell。我无法得到任何工作。这是迄今为止我的自定义 NSCell 中最好的:

- (void)drawInteriorWithFrame:(NSRect)frame inView:(NSView *)controlView { 
if (thumbnailLink) {
NSURL *url = [NSURL URLWithString:thumbnailLink];
if (url) {
NSRect imageFrame = [self _imageFrameForInteriorFrame:frame];
NSImage *image = [[NSImage alloc] initWithContentsOfURL:url];
[image setScalesWhenResized:YES];
[image setSize:NSMakeSize(IMAGE_HEIGHT, IMAGE_WIDTH)];

[NSGraphicsContext saveGraphicsState];
imageFrame = NSInsetRect(imageFrame, 1, 1);
NSBezierPath *clipPath = [NSBezierPath bezierPathWithRoundedRect:imageFrame cornerRadius:5.0];
[clipPath setWindingRule:NSEvenOddWindingRule];
[clipPath addClip];
[NSGraphicsContext restoreGraphicsState];
[image drawInRect:imageFrame fromRect:NSMakeRect(0, 0, 0, 0) operation:NSCompositeSourceIn fraction:1.0];
[image release];
}
}
...

关于如何做到这一点有什么想法吗?

最佳答案

您需要在绘制图像时保留剪辑集,并在之后恢复上下文。另外,如果您只想正常绘制图像而不考虑目标不透明度,我认为您不想使用“in”合成,而是使用“over”合成。尝试这样的事情:

[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];

关于cocoa - 如何绘制圆形 NSImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1849636/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com