【问题标题】:Using layer backed NSView as NSDockTile contentView使用层支持的 NSView 作为 NSDockTile contentView
【发布时间】:2012-02-09 08:08:29
【问题描述】:
有没有办法使用支持NSView 的层作为NSDockTile 的contentView?尝试了各种技巧,但我得到的只是透明区域。还尝试了不同的路线并从CALayer 中获取图像并将其用于[NSApp setApplicationIconImage:],但也没有运气 - 我认为这里的问题是为屏幕外图像创建图像表示。
【问题讨论】:
标签:
calayer
nsview
nsimage
nsimagerep
nsdocktile
【解决方案1】:
像往常一样,我在发布问题后很快就得到了答案 :) 我将其发布在这里以供将来参考:我通过在层外创建 NSImage 来解决它,如 Cocoa is my girl blog post here @ 中所述987654321@
唯一缺少的部分是,为了渲染任何内容,必须将视图添加到窗口中,因此使用帖子中的示例代码,我的解决方案是:
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];