有没有办法使用支持NSView
的层作为contentView
a NSDockTile
?尝试了各种技巧,但我得到的只是透明区域。还尝试了不同的路线并从中获取图像CALayer
并将其用于[NSApp setApplicationIconImage:]
,但也没有运气 - 我认为这里的问题是为屏幕外图像创建图像表示。
问问题
911 次
1 回答
2
像往常一样,我在发布问题后很快就得到了答案:) 我会在这里发布以供将来参考:我通过创建NSImage
层来解决它,如 Cocoa 中所述,这是我的女朋友博客文章http://www.cimgf .com/2009/02/03/record-your-core-animation-animation/
唯一缺少的部分是,为了渲染任何内容,必须将视图添加到窗口中,因此使用帖子中的示例代码,我的解决方案是:
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];
于 2012-02-10T09:03:28.147 回答