Why does the iphone CGImageCreate with data from standard BMP bits give the wrong result?

半腔热情 提交于 2019-12-08 14:32:23

问题


i have a gdi engine that result in the standard bmp bits stream ,and now i wanna to display it by the CG in iphone , i use it like this:

// size  -> image size 
// rect  -> current view rect
// pBits -> the BITMAPINFO image bits stream

long imgSizePerRow = ((long)(24 * size.width + 31) / 32) * 4;
CGDataProviderRef providerRef = CGDataProviderCreateWithData(NULL, pBits,
                                          imgSizePerRow * size.height, NULL);

CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();

CGImageRef imageRef = CGImageCreate(size.width, size.height, 8, 24,
                                    imgSizePerRow, colorSpaceRef,
                                    kCGBitmapByteOrderDefault, providerRef,
                                    NULL, YES, kCGRenderingIntentDefault);

CGContextDrawImage(context, rect, imageRef);

CGImageRelease(imageRef);
CGColorSpaceRelease(colorSpaceRef);
CGDataProviderRelease(providerRef);

attention: i check the row pixel fit to the times of 4

My gdi generate the bits stream , in this case , R8G8B8 format , no alpha channel ,i use this for CGDataProviderRef and draw the CGImage to my view. it is strange that the image's color seems change, yet the whole shape seems ok , i save the bits stream by BITMAP FORMAT to a file before, and make a contrast , everything seems ok just the whole color space.

CAN anyone tell me where is my fault in the code, or i miss some para. setting?


回答1:


CGImageCreate takes raw pixels, which a .bmp file is not (it has that “BITMAPINFO” header). Instead, create a UIImage with the .bmp data, then get the CGImage from that object.




回答2:


I missed the important point: my GDI bitmap stream is sequenced by B8->G8->R8.



来源:https://stackoverflow.com/questions/755316/why-does-the-iphone-cgimagecreate-with-data-from-standard-bmp-bits-give-the-wron

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!