Why does the iphone CGImageCreate with data from s

2019-06-10 16:06发布

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?

2条回答
劫难
2楼-- · 2019-06-10 16:34

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

查看更多
Viruses.
3楼-- · 2019-06-10 16:39

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.

查看更多
登录 后发表回答