有没有办法使用层支持的NSView
作为NSDockTile
的contentView
?尝试了各种技巧,但我得到的只是透明区域。还尝试采用不同的路线并从 CALayer
中获取图像并将其用于 [NSApp setApplicationIconImage:]
,但也没有运气 - 我认为这里的问题是创建图像屏幕外图像的表示。
最佳答案
像往常一样,我在发布问题后很快就得到了答案:)我将其发布在这里以供将来引用:我通过在图层之外创建 NSImage
来解决它,如 Cocoa is my 中所述女朋友博客在这里http://www.cimgf.com/2009/02/03/record-your-core-animation-animation/
唯一缺少的部分是,为了渲染任何内容,必须将 View 添加到窗口中,因此使用帖子中的示例代码,我的解决方案是:
NSView *myView = ...
NSWindow *window = [[NSWindow alloc] initWithContentRect:NSMakeRect(-1000.0, -1000.0, 256.0, 256.0) styleMask:0 backing:NSBackingStoreNonretained defer:NO];
[window setContentView:myView];
NSUInteger pixelsHigh = myView.bounds.size.height;
NSUInteger pixelsWide = myView.bounds.size.width;
NSUInteger bitmapBytesPerRow = pixelsWide * 4;
CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
CGContextRef context = CGBitmapContextCreate(NULL, pixelsWide, pixelsHigh, 8, bitmapBytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast);
CGColorSpaceRelease(colorSpace);
[myView.layer.presentationLayer renderInContext:context];
CGImageRef image = CGBitmapContextCreateImage(context);
NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc] initWithCGImage:image];
CFRelease(image);
NSImage *img = [[NSImage alloc] initWithData:[bitmap TIFFRepresentation]];
[NSApp setApplicationIconImage:img];
关于calayer - 使用图层支持的 NSView 作为 NSDockTile contentView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9207319/