4

Possible Duplicate:
I want capture screen when any game is running with opengl

I am developing a action game with OpenGL.
When a best shot play by user I want to capture them and upload to social network.
I used many code but no success with then. Some code are on:

OpenGL ES View Snapshot

       NSInteger myDataLength = 480 * 320 * 4;

        // allocate array and read pixels into it.
        GLubyte *buffer = (GLubyte *) malloc(myDataLength);
        glReadPixels(0, 0, 480, 320, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

        // gl renders "upside down" so swap top to bottom into new array.
        // there's gotta be a better way, but this works.
        GLubyte *buffer2 = (GLubyte *) malloc(myDataLength);
        for(int y = 0; y < 320; y++)
        {
            for(int x = 0; x < 480 * 4; x++)
            {
                buffer2[(319 - y) * 480 * 4 + x] = buffer[y * 4 * 480 + x];
            }
        }


        // make data provider with data.
        CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, myDataLength, NULL);

        // prep the ingredients
        int bitsPerComponent = 8;
        int bitsPerPixel = 32;
        int bytesPerRow = 4 * 480;
        CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
        CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
        CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;

        // make the cgimage
        CGImageRef imageRef = CGImageCreate(480, 320, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO,   renderingIntent);
UIImage *image = [UIImage imageWithCGImage:imageRef];

And Other Code Is :

CGRect screenRect = [[UIScreen mainScreen] bounds];
UIGraphicsBeginImageContextWithOptions(screenRect.size, NO, 0.0);
[glview.parent.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

But All Code Generate Image In Simulator Not In Device(Black or white image only)

Community
  • 1
  • 1

1 Answers1

0

This question is already answered here. Here is what I believe you are looking for

How to get UIImage from EAGLView?

Community
  • 1
  • 1