iphone - 以编程方式获取结合 OpenGL 和 UIKit 元素的屏幕截图

标签 iphone objective-c ios5 screenshot

我想知道是否有人可以提供一个示例,说明如何截取混合了 OpenGL 和 UIKit 元素的屏幕截图。自从 Apple 将 UIGetScreenImage() 设为私有(private)以来,这就变成了一项相当困难的任务,因为 Apple 用来替换它的两种常用方法仅捕获 UIKit 或仅捕获 OpenGL。

This类似问题引用Apple's Technical Q&A QA1714 ,但 QA 只描述了如何处理来自相机和 UIKit 的元素。您如何将 UIKit View 层次结构渲染到图像上下文中,然后像类似问题的答案所建议的那样在其上绘制 OpenGL ES View 的图像?

最佳答案

这应该可以解决问题。基本上将所有内容渲染成 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];

关于iphone - 以编程方式获取结合 OpenGL 和 UIKit 元素的屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11299682/

相关文章:

ios - 如何从 ios 中的 UIImagePickerController 中选择多个图像

iphone - 在 iPhone 核心数据中的一对多关系上使用级联删除规则和 validateForDelete

iphone - 在iphone中使用json解析文本文件

iphone - CMBufferQueueCreate 需要哪些参数?

ios - NSDictionary 的 NSArray - 当数组中只有一个字典而不是一个对象的数组时返回 NSDictionary

ios - 如何使 UITextView 在方向改变时调整大小?

iphone - NSPredicate - 评估两个条件

ios - 如何使 iOS 7 内置的 iOS 6 兼容应用程序?

iphone - View Controller 包含与直接 View 操作

ios - 如何在 iOS 上创建 Flap-up 信息页面