我打算将UIView
绘制的OpenGL ES
的快照拍摄到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);
之前使用return
,但这会引发BAD_ACCESS
运行时错误。
所以我的问题是,free
如何正确?或者我应该在图像发布后free
使用它们吗?
答案 0 :(得分:1)
您应该使用here所述的CGDataProviderCreateWithData's
CGDataProviderReleaseDataCallback
。
只需添加freeData函数回调:
void freeData(void *info, const void *data, size_t size)
{
free((void*)data);
}
并将其传递给CGDataProviderCreateWithData
:
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, data, dataLength, freeData);
答案 1 :(得分:0)
这是我目前的代码,它运行得很好。
if(context){
[EAGLContext setCurrentContext:context];
glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer);
NSInteger dataLength = width * height * 4;
int bitsPerComponent = 8;
int bitsPerPixel = 32;
int bytesPerRow = 4 * width;
GLubyte* data = (GLubyte*)malloc(dataLength) ;
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data);
// make upside down
GLubyte* swp = (GLubyte*)malloc(width * 4);
for (int h = 0; h < height / 2; h++) {
memcpy(swp, data + (height - 1 - h) * bytesPerRow, bytesPerRow);
memcpy(data + (height - 1 - h) * bytesPerRow, data + h * bytesPerRow, bytesPerRow);
memcpy(data + h * bytesPerRow, swp, bytesPerRow);
}
NSData* nsData = [NSData dataWithBytesNoCopy:data length:dataLength freeWhenDone:YES];
// make data provider with data.
CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)nsData);
// prep the ingredients
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
// make the cgimage
CGImageRef result = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
CGColorSpaceRelease(colorSpaceRef);
CGDataProviderRelease(provider);
// then make the uiimage from that
UIImage *image = [UIImage imageWithCGImage:result];
CGImageRelease(result);
return image;
}