编程采取截图结合的OpenGL和UIKit元素(Programmatically take a sc

2019-07-30 05:30发布

我想知道如果任何人都可以提供如何拍摄它融合的OpenGL和UIKit元素的屏幕截图的例子。 自从苹果提出UIGetScreenImage()私人因为苹果用于替代两种常见的方法是只捕获的UIKit或仅OpenGL的这已经成为一个相当艰巨的任务。

这种类似的问题,引用了苹果的技术Q&A QA1714 ,但仅进行质量检查介绍如何处理从相机和UIKit元素。 你如何去渲染的UIKit视图层次成图像背景,然后绘制你的OpenGL ES视角的图像在它上面像答案类似的问题提出?

Answer 1:

这应该做的伎俩。 基本上呈现一切CG和创建你可以做任何的图像。

// In Your UI View Controller

- (UIImage *)createSavableImage:(UIImage *)plainGLImage
{    
    UIImageView *glImage = [[UIImageView alloc] initWithImage:[myGlView drawGlToImage]];
    glImage.transform = CGAffineTransformMakeScale(1, -1);

    UIGraphicsBeginImageContext(self.view.bounds.size);

    //order of getting the context depends on what should be rendered first.
    // this draws the UIKit on top of the gl image
    [glImage.layer renderInContext:UIGraphicsGetCurrentContext()];
    [someUIView.layer renderInContext:UIGraphicsGetCurrentContext()];

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

    // Do something with resulting image 
    return finalImage;
}

// In Your GL View

- (UIImage *)drawGlToImage
{
    // Draw OpenGL data to an image context 

    UIGraphicsBeginImageContext(self.frame.size);

    unsigned char buffer[320 * 480 * 4];

    CGContextRef aContext = UIGraphicsGetCurrentContext();

    glReadPixels(0, 0, 320, 480, GL_RGBA, GL_UNSIGNED_BYTE, &buffer);

    CGDataProviderRef ref = CGDataProviderCreateWithData(NULL, &buffer, 320 * 480 * 4, NULL);

    CGImageRef iref = CGImageCreate(320,480,8,32,320*4, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaLast, ref, NULL, true, kCGRenderingIntentDefault);

    CGContextScaleCTM(aContext, 1.0, -1.0);
    CGContextTranslateCTM(aContext, 0, -self.frame.size.height);

    UIImage *im = [[UIImage alloc] initWithCGImage:iref];

    UIGraphicsEndImageContext();

    return im;
}

然后,要创建一个截图

UIImage *glImage = [self drawGlToImage];
UIImage *screenshot = [self createSavableImage:glImage];


文章来源: Programmatically take a screenshot combining OpenGL and UIKit elements