2012-02-09 47 views
2

有沒有辦法使用支持NSView的圖層作爲NSDockTilecontentView?嘗試了各種各樣的技巧,但我得到的只是透明區域。也嘗試去不同的路線,並從CALayer得到一個圖像,並將其用於[NSApp setApplicationIconImage:],但沒有運氣 - 我認爲這裏的問題是爲離屏圖像創建圖像表示。使用分層NSView作爲NSDockTile contentView

回答

2

像往常一樣,我有我的答案即將發佈問題後:)我將它張貼在這裏以供將來參考:我解決它通過在可可描述創建NSImage走出層的是我的女朋友此處的博客文章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];