Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
581 views
in Technique[技术] by (71.8m points)

cocoa - CGImage from byte array

Loading a CGImage or NSImage from a file using a standard image format (jpeg, gif, png et.) is all very simple.

However, I now need to create a CGImage from an array in bytes in memory generated using libfreetype. Its really easy to create OpenGL textures from an array of formatted bytes, and I can see how to create a CGBitmapContext to write to. But I can't seem to find an easy way to create a CGImage from a raw pixel array.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Using CFData can make the above code simpler. Here is the code.

// raw pixel data memory of 64 * 64 pixel size

UInt8 pixelData[64 * 64 * 3];

// fill the raw pixel buffer with arbitrary gray color for test

for(size_t ui = 0; ui < 64 * 64 * 3; ui++)
  pixelData[ui] = 210;

CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();

CFDataRef rgbData = CFDataCreate(NULL, pixelData, 64 * 64 * 3);

CGDataProviderRef provider = CGDataProviderCreateWithCFData(rgbData);

CGImageRef rgbImageRef = CGImageCreate(64, 64, 8, 24, 64 * 3, colorspace, kCGBitmapByteOrderDefault, provider, NULL, true, kCGRenderingIntentDefault); 

CFRelease(rgbData);

CGDataProviderRelease(provider);

CGColorSpaceRelease(colorspace);

// use the created CGImage

CGImageRelease(rgbImageRef);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...