iPhone:如何在内存中创建一个空位图?

时间:2011-05-16 17:27:09

标签: iphone

我需要创建一个具有已知宽度和高度值的空位图(在内存中)。 我将使用此位图的上下文绘制然后在MKOverlayVew上显示图像。 有谁知道最简单的方法吗?

1 个答案:

答案 0 :(得分: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提到了内存问题,我从来没有使用过我发布的代码。