我正在尝试从 NSTextField 创建 CGImage。
我在这方面取得了一些成功。我仍然无法获得仅包含文本的 CGImage。我的意思是说,每次捕获文本字段时,我都会得到背景窗口的颜色。(看起来我没有得到 alpha 通道信息)
我尝试了以下来自http://www.cocoadev.com/index.pl?ConvertNSImageToCGImage的片段
NSBitmapImageRep * bm = [NSBitmapImageRep alloc];
[theView lockFocus];
[bitmap initWithFocusedViewRect:[theView bounds]];
[theView unlockFocus]
[bma retain];// data provider will release this
int rowBytes, width, height;
rowBytes = [bm bytesPerRow];
width = [bm pixelsWide];
height = [bm pixelsHigh];
CGDataProviderRef provider = CGDataProviderCreateWithData( bm, [bm bitmapData], rowBytes * height, BitmapReleaseCallback );
CGColorSpaceRef colorspace = CGColorSpaceCreateWithName( kCGColorSpaceGenericRGB );
CGBitmapInfo bitsInfo = kCGImageAlphaPremultipliedLast;
CGImageRef img = CGImageCreate( width, height, 8, 32, rowBytes, colorspace, bitsInfo, provider, NULL, NO, kCGRenderingIntentDefault );
CGDataProviderRelease( provider );
CGColorSpaceRelease( colorspace );
return img;
获得没有背景颜色的 CGImage 有什么帮助吗?