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


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);


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?

Best Solution

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.