2011-05-16 52 views
0

我需要创建一个空的位图(在存储器中)与宽度和高度的已知值。 我将使用此位图的上下文在MKOverlayVew上绘制并显示图像。 有谁知道最简单的方法来做到这一点?iPhone:如何在内存中创建一个空位图?

+0

空如在透明?或“空”,如填充一些背景颜色(黑色,白色等)? – hotpaw2 2011-05-16 19:12:13

回答

1

下面的代码应该做的伎俩。原籍: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); 

我不知道是否有与贴在这个问题的代码有什么区别,但OP提到内存的问题,我从未有过任何与此代码我张贴。

+0

谢谢!这段代码真的有用。并且运作良好。结果,我的应用程序性能提高了近100倍。 – martianex 2011-05-17 16:16:32

+0

一旦你调用CGBitmapContextCreate,你还可以通过编写inte rawData内存来修改像素吗? – webshaker 2012-07-12 12:58:05