Cocos2D should have a function for capture the OpenGL image in screen.
I developed a function for this.
- (Texture2D*) cocos2DScreenShot {
CGSize size = [self displaySize];
//Create un buffer for pixels
int bufferLenght=size.width*size.height*4;
unsigned char buffer[bufferLenght];
//Read Pixels from OpenGL
glReadPixels(0,0,size.width,size.height,GL_RGBA,GL_UNSIGNED_BYTE,&buffer);
CGDataProviderRef ref = CGDataProviderCreateWithData(NULL, &buffer, bufferLenght, NULL);
CGImageRef iref = CGImageCreate(size.width,size.height,8,32,size.width*4,CGColorSpaceCreateDeviceRGB(),kCGBitmapByteOrderDefault,ref,NULL,true,kCGRenderingIntentDefault);
uint32_t *pixels = (uint32_t *)malloc(bufferLenght);
CGContextRef context = CGBitmapContextCreate(pixels, [self winSize].width, [self winSize].height, 8, [self winSize].width*4, CGImageGetColorSpace(iref), kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGContextTranslateCTM(context,0, size.height);
CGContextScaleCTM(context, 1.0, -1.0);
switch (deviceOrientation_) {
case CCDeviceOrientationPortrait:
break;
case CCDeviceOrientationPortraitUpsideDown:
CGContextRotateCTM(context, CC_DEGREES_TO_RADIANS(180));
CGContextTranslateCTM(context,-size.width, -size.height);
break;
case CCDeviceOrientationLandscapeLeft:
CGContextRotateCTM(context, CC_DEGREES_TO_RADIANS(-90));
CGContextTranslateCTM(context,-size.height, 0);
break;
case CCDeviceOrientationLandscapeRight:
CGContextRotateCTM(context, CC_DEGREES_TO_RADIANS(90));
CGContextTranslateCTM(context,size.width*0.5, -size.height);
break;
}
CGContextDrawImage(context, CGRectMake(0.0, 0.0, size.width, size.height), iref);
UIImage *outputImage = [UIImage imageWithCGImage:CGBitmapContextCreateImage(context)];
Texture2D *texture = [[Texture2D alloc] initWithImage:outputImage];
//Dealloc
CGDataProviderRelease(ref);
CGImageRelease(iref);
CGContextRelease(context);
free(pixels);
return texture;
}
I found the original code in: http://stackoverflow.com/questions/962390/capturing-eaglview-content-with-alpha-channel-on-iphone
but I edited it because didn't support orientation and it didn't release the memory.
I open a inssue in the cocos2d-iphone Project:
http://code.google.com/p/cocos2d-iphone/issues/detail?id=533