我打算将 aUIView
绘制OpenGL ES
到a 中的快照UIImage
,这是我使用的代码:
if(context){
[EAGLContext setCurrentContext:context];
glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer);
NSInteger dataLength = framebufferWidth * framebufferHeight * 4;
GLubyte* data = (GLubyte*)malloc(dataLength) ; // malloc(myDataLength);
glReadPixels(0, 0, framebufferWidth, framebufferHeight, GL_RGBA, GL_UNSIGNED_BYTE, data);
// make data provider with data.
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, data, dataLength, NULL);
// prep the ingredients
int bitsPerComponent = 8;
int bitsPerPixel = 32;
int bytesPerRow = 4 * framebufferWidth;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
// make the cgimage
CGImageRef result = CGImageCreate(framebufferWidth, framebufferHeight, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
// then make the uiimage from that
UIImage *image = [UIImage imageWithCGImage:result];
CGImageRelease(result);
CGColorSpaceRelease(colorSpaceRef);
CGDataProviderRelease(provider);
//free(data);
return image;
}
我本来打算使用free(data);
before return
,但这会引发BAD_ACCESS
运行时错误。
所以我的问题是,如何free
正确?或者我应该free
在图像发布后他们?