take screen Programmatically of UIview+glview

前端 未结 2 1191
温柔的废话
温柔的废话 2021-01-16 23:51

i have glview in my uiview ,now i have to take scrren shot of combine view of uiview and glview. i googled lot but i dnt found any thing useful i know how to take scrrenshot

相关标签:
2条回答
  • 2021-01-17 00:19

    It seems like it's pretty tricky to get a screenshot nowadays, especially when you're mixing the UIKit and OpenGL ES: there used to be UIGetScreenImage() but Apple made it private again and is rejecting apps that use it.

    Instead, there are two "solutions" to replace it: Screen capture in UIKit applications and OpenGL ES View Snapshot. The former does not capture OpenGL ES or video content while the later is only for OpenGL ES.

    There is another technical note How do I take a screenshot of my app that contains both UIKit and Camera elements?, and here they essentially say: You need to first capture the camera picture and then when rendering the view hierarchy, draw that image in the context.

    The very same would apply for OpenGL ES: You would first need to render a snapshot for your OpenGL ES view, then render the UIKit view hierarchy into an image context and draw the image of your OpenGL ES view on top of it. Very ugly, and depending on your view hierarchy it might actually not be what you're seeing on screen (e. g. if there are views in front of your OpenGL view).

    0 讨论(0)
  • 2021-01-17 00:20

    Inspired by DarkDust, I was successful in implementing a screen capture of a mix of uiview and openglview (cocos2d 2.0 view). I've sanitized the code a bit and pasted below, hopefully it's helpful for others.

    To help explain the setup, my app screen has 4 view layers: the back is a background UIView with background images "backgroundLayer", the middle are 2 layers of Cocos2d glview "glLayer1 and glLayer2"; and the front is another UIView layer with a few native UI controls (e.g. UIButtons) "frontView".

    Here's the code:

    + (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;
    }
    
    0 讨论(0)
提交回复
热议问题