Escape programmatically UIview + glview

I have glview in my uiview, now I need to take a snapshot with a combination of uiview and glview view. I googled a lot, but I did not find anything useful, I know how to take a scrrenshot glview

nt width = glView.frame.size.width; int height = glView.frame.size.height;

NSInteger myDataLength = width * height * 4;
// allocate array and read pixels into it.
GLubyte *buffer = (GLubyte *) malloc(myDataLength);
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
// gl renders "upside down" so swap top to bottom into new array.
// there gotta be a better way, but this works.
GLubyte *buffer2 = (GLubyte *) malloc(myDataLength);
for(int y = 0; y < height; y++)
{
    for(int x = 0; x < width * 4; x++)
    {
        buffer2[((height - 1) - y) * width * 4 + x] = buffer[y * 4 * width + 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 * width;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
// make the cgimage
CGImageRef imageRef = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
// then make the uiimage from that
UIImage *myImage = [UIImage imageWithCGImage:imageRef];
return myImage;
0
source share
2 answers

It seems now it’s quite difficult to get a screenshot, especially when you mix UIKit and OpenGL ES: it used to be UIGetScreenImage(), but Apple closed it again and refuses applications that use it.

"" : UIKit OpenGL ES . OpenGL ES , - OpenGL ES.

, UIKit Camera?, : , .

OpenGL ES: OpenGL ES, UIKit OpenGL ES . , , (, OpenGL ).

+5

DarkDust, uiview openglview ( cocos2d 2.0). , , .

, 4 : - UIView "backgroundLayer", - 2 Cocos2d glview "glLayer1 glLayer2"; - UIView (, UIButtons) "frontView".

:

+ (UIImage *) grabScreenshot
{
    // Get the 2 layers in the middle of cocos2d glview and store it as UIImage
    [CCDirector sharedDirector].nextDeltaTimeZero = YES;

    CGSize winSize = [CCDirector sharedDirector].winSize;
    CCRenderTexture* rtx =
        [CCRenderTexture renderTextureWithWidth:winSize.width
                                         height:winSize.height];
    [rtx begin];
    [glLayer1 visit];
    [glLayer2 visit];
    [rtx end];

    UIImage *openglImage = [rtx getUIImage];

    UIGraphicsBeginImageContext(winSize);

    // Capture the bottom layer
    [backgroundView.layer renderInContext:UIGraphicsGetCurrentContext()];
    // Save the captured glLayers image to the image context
    [openglImage drawInRect:CGRectMake(0, 0, openglImage.size.width, openglImage.size.height)];
    [frontView.layer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return viewImage;
}
+1

All Articles