iPhone: how to create an empty bitmap in memory?
I need to create a empt开发者_C百科y bitmap (in memory) with known values of the width and height. I'll use context of this bitmap to draw and then display image on the MKOverlayVew. Does anyone know the easiest way to do this?
The following code should do the trick. Originally from: CGBitmapContextCreate on the iPhone/iPad
int width = 500;
int height = 500;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = malloc(height * width * 4);
memset(rawData, 0, height * width * 4);
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData, width, height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
// Do your thing and then release
CGColorSpaceRelease(colorSpace);
free(rawData);
CGContextRelease(context);
I'm not sure if there's any difference with the code posted on that question, but the OP mentions memory issues and I never had any with this code I'm posting.
精彩评论