glReadPixedls return black

Hi all,

in my Unity application, I want to capture the screenshot. So I use following code (as a plugin for Unity)

+(void)shot:(id)sender
{

CGRect rect = [[UIScreen mainScreen] bounds];
int width = rect.size.width;
int height = rect.size.height;

NSInteger dataLength = width * height * 4;
GLubyte *buffer  = (GLubyte *) malloc(dataLength);
GLubyte *buffer2 = (GLubyte *) malloc(dataLength);

glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
for (int y = 0; y < height; y++) {
    for (int x = 0; x < width; x++) {
        buffer2[((height - 1 - y) * width * 4 + x)] = buffer[(y * 4 * width + x)];
    }
}

free(buffer);

CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, dataLength, releaseData);

int bitsPerComponent = 8;
int bitsperPixel = 32;
int bytesPerRow = 4 * width;

CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
CGImageRef imageRef = CGImageCreate(width, height, bitsPerComponent, bitsperPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);

CGColorSpaceRelease(colorSpaceRef);
CGDataProviderRelease(provider);

UIImage *image = [[UIImage alloc] initWithCGImage:imageRef];
CGImageRelease(imageRef);

UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

}

Everything works ok. Only that the screenshot picture is whole black(my scene has contents of course). I googled to find a similar question here:

but since it is for Cocos2D, not Unity, I don’t know where to apply the solution to my Unity project. Need your help, please!

PS.

Unity version 4.0, ios version 6.1.

This is known unity’s bug. It happends becouse ios doesn’t has main framebuffer like in the other systems. And unity do something like this:

glBindFramebuffer(GL_FRAMEBUFFER, 0)

In other systems zero framebuffer - main framebuffer, but in ios - no.
So you have to manualy bind active framebuffer, in my case it is 1st:

glBindFramebuffer(GL_FRAMEBUFFER, 1)