我想从NSTextField创建CGImage。
我在这方面取得了一些成功。我仍然不能获得只包含文本的CGImage。我的意思是说,每捕获文本字段我得到的背景画面的颜色与它一起。时间(貌似我没有得到alpha通道信息)从http://www.cocoadev.com/index.pl?ConvertNSImageToCGImage从NSView中检索CGImage
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的帮助?
其实,我要从CGImage生成纹理。无论如何感谢你的soln,我很快就会回来。 – shakthi 2010-08-06 17:18:55
这很好。以上所有方法都会生成一个NSImage或NSBitmapImageRep,您可以从中创建CGImage。 – kperryua 2010-08-06 19:39:29
其工作正常,谢谢。 – shakthi 2010-08-09 05:04:46