操作图像时的OutOfMemory

时间:2012-05-19 10:27:59

标签: processing out-of-memory

运行几秒后会产生OutOfMemory异常。有什么想法吗?

PGraphics img;

void setup() {
  size(500, 500);
  img = createGraphics(width, height, JAVA2D);

  // this is here just for the testcase because else I get a
  // NullPointerException too (probably a harmless Processing bug)
  img.beginDraw(); img.endDraw();
}

void draw() {
  PGraphics tmpImg = createGraphics(img.width, img.height, JAVA2D);
  tmpImg.beginDraw();
  tmpImg.image(img, 0, 0);
  tmpImg.endDraw();
  tmpImg.dispose();
}

1 个答案:

答案 0 :(得分:2)

Mat是的,你不应该在每一帧中实例化一个新的PGraphics。 你可以简单地做这样的事情:

PGraphics img;

void setup() {
  size(500, 500);
  img = createGraphics(width, height, JAVA2D);

  // this is here just for the testcase because else I get a
  // NullPointerException too (probably a harmless Processing bug)
  img.beginDraw(); img.endDraw();
}

void draw() {
  image(img, 0, 0);
}

因为PGraphics扩展了PImage。 通常你会使用basic PImage API,但如果你需要在位图上绘制形状或伪造'层',你可以将PGraphics与PImage结合使用,但不要将新的PGraphics 30分配到每秒60次。