以编程方式获取UIview + glview

时间:2011-06-01 06:30:47

标签: iphone opengl-es

我在我的uiview中有glview,现在我必须拍摄uiview和glview的组合视图。 我google了很多,但我发现任何有用的东西,我知道如何采取glview的scrrenshot

  

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's 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;

2 个答案:

答案 0 :(得分:5)

现在获取屏幕截图似乎非常棘手,特别是当您混合使用UIKit和OpenGL ES时:曾经有UIGetScreenImage()但是Apple made it private again并拒绝使用它的应用程序。

相反,有两个“解决方案”可以取代它:Screen capture in UIKit applicationsOpenGL ES View Snapshot。前者不捕获OpenGL ES或视频内容,而后者用于OpenGL ES。

还有另一个技术说明How do I take a screenshot of my app that contains both UIKit and Camera elements?,在这里他们基本上说:您需要首先捕获相机图片,然后在渲染视图层次结构时,在上下文中绘制该图像。

同样适用于OpenGL ES:您首先需要为OpenGL ES视图渲染快照,然后将UIKit视图层次结构渲染到图像上下文中,并在其上绘制OpenGL ES视图的图像。非常难看,并且根据您的视图层次结构,它实际上可能不是您在屏幕上看到的内容(例如,如果您的OpenGL视图前面有视图)。

答案 1 :(得分:1)

受DarkDust的启发,我成功实现了uiview和openglview混合的屏幕截图(cocos2d 2.0视图)。我已经对代码进行了清理并粘贴在下面,希望它对其他人有用。

为了帮助解释设置,我的应用程序屏幕有4个视图层:背面是背景图像“backgroundLayer”的背景UIView,中间是2层Cocos2d glview“glLayer1和glLayer2”;前面是另一个UIView层,带有一些原生UI控件(例如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;
}