gpt4 book ai didi

cocoa - 从 NSView 中制作 NSImage

转载 作者:行者123 更新时间:2023-12-03 17:50:02 24 4
gpt4 key购买 nike

我知道如何创建一个描绘 NSView 及其所有 subview 的 NSImage ,但我想要的是一个 NSImage 的 View 忽略其 subview 。我可以想出使用 NSView 的子类来实现此目的的方法,但如果可能的话,我希望避免子类化。有人有什么想法吗?

最佳答案

隐藏 subview ,抓取图像,取消隐藏 subview :

NSMutableArray* hiddenViews = [[NSMutableArray] alloc init];

for (NSView* subview in [self subviews]) {
if (subview hidden) [hiddenViews addObject: subview];
else [subview setHidden:YES];
}

NSSize imgSize = self.bounds.size;
NSBitmapImageRep * bir = [self bitmapImageRepForCachingDisplayInRect:[self bounds]];
[bir setSize:imgSize];
[self cacheDisplayInRect:[self bounds] toBitmapImageRep:bir];
NSImage* image = [[NSImage alloc] initWithSize:imgSize];
[image addRepresentation:bir];

for (NSView* subview in [self subviews]) {
if (![hiddenViews containsObject: subview])
[subview setHidden:NO];
}

关于cocoa - 从 NSView 中制作 NSImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29752657/

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