我使用过Apple的GLImageProcessing示例-在该示例中,我对图像应用了各种过滤器。我遵循了GLImageProcessing多重过滤器?使用两个过滤器
苹果最初的例子在不泄漏内存方面做得很好。
使用上面提到的问题中的代码使应用程序在使用10-20秒内消耗所有iPhone内存,给我一个内存警告,最终使应用程序崩溃。
我知道这与创建额外的纹理和帧缓冲对象(FBO的)有关,但我不确定如何正确管理它们(和它们的内存)
代码:
void drawGL(int wide, int high, float contrastVal,float brightnessVal)
{
//INIT
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrthof(0, wide, 0, high, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glScalef(wide, high, 1);
glGetIntegerv(GL_FRAMEBUFFER_BINDING_OES, (GLint *)&SystemFBO);
// Create the texture and the FBO the will hold the result of applying the first filter
glGenTextures(1, &ResultTexture);
glBindTexture(GL_TEXTURE_2D, ResultTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, wide, high, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glGenFramebuffersOES(1, &ResultTextureFBO);
glBindFramebufferOES(GL_FRAMEBUFFER_OES, ResultTextureFBO);
glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, ResultTexture, 0);
glBindFramebufferOES(GL_FRAMEBUFFER_OES, ResultTextureFBO);
glBindTexture(GL_TEXTURE_2D, Input.texID);
glViewport(0, 0, wide, high);
brightness(flipquad, contrastVal);
glCheckError();;
glBindFramebufferOES(GL_FRAMEBUFFER_OES, SystemFBO);
glBindTexture(GL_TEXTURE_2D, ResultTexture);
glViewport(0, 0, wide, high);
contrast(fullquad,brightnessVal);
glCheckError();
}
有更好的方法吗?
你所做的看起来很好,但你永远不会删除纹理或fbo。您需要调用:
glDeleteTextures (1, ResultTexture);
glDeleteBuffers (1, ResultTextureFBO);
释放它们所使用的内存。否则,它们会永远在你身边。
我遇到了完全相同的问题。内存警告似乎是每次调用该方法时生成纹理的结果。
因此,尝试将以下行移动到示例代码的initGL方法:glGenTextures(1, &ResultTexture);
glGenFramebuffersOES(1, &ResultTextureFBO);