之前写过一个常规的屏幕截图:http://blog.csdn.net/hitwhylz/article/details/17189351

可是发现这个办法对于OpenGL 无用。  获取到的数据为空。

所以这里介绍下OpenGL ES屏幕截图。

1.初始化。

CAEAGLLayer *eaglLayer = (CAEAGLLayer *) self.layer;
eaglLayer.drawableProperties = @{
kEAGLDrawablePropertyRetainedBacking: [NSNumber numberWithBool:YES],
kEAGLDrawablePropertyColorFormat: kEAGLColorFormatRGBA8
};

2.声明方法。

- (UIImage*)snapshot:(UIView*)eaglview
{
GLint backingWidth, backingHeight; // Bind the color renderbuffer used to render the OpenGL ES view // If your application only creates a single color renderbuffer which is already bound at this point, // this call is redundant, but it is needed if you're dealing with multiple renderbuffers. // Note, replace "viewRenderbuffer" with the actual name of the renderbuffer object defined in your class. glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer); // Get the size of the backing CAEAGLLayer glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth); glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight); NSInteger x = 0, y = 0, width = backingWidth, height = backingHeight; NSInteger dataLength = width * height * 4; GLubyte *data = (GLubyte*)malloc(dataLength * sizeof(GLubyte)); // Read pixel data from the framebuffer glPixelStorei(GL_PACK_ALIGNMENT, 4); glReadPixels(x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data); // Create a CGImage with the pixel data // If your OpenGL ES content is opaque, use kCGImageAlphaNoneSkipLast to ignore the alpha channel // otherwise, use kCGImageAlphaPremultipliedLast CGDataProviderRef ref = CGDataProviderCreateWithData(NULL, data, dataLength, NULL); CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB(); CGImageRef iref = CGImageCreate(width, height, 8, 32, width * 4, colorspace, kCGBitmapByteOrder32Big | kCGImageAlphaPremultipliedLast, ref, NULL, true, kCGRenderingIntentDefault); // OpenGL ES measures data in PIXELS // Create a graphics context with the target size measured in POINTS NSInteger widthInPoints, heightInPoints; if (NULL != UIGraphicsBeginImageContextWithOptions) { // On iOS 4 and later, use UIGraphicsBeginImageContextWithOptions to take the scale into consideration // Set the scale parameter to your OpenGL ES view's contentScaleFactor // so that you get a high-resolution snapshot when its value is greater than 1.0 CGFloat scale = eaglview.contentScaleFactor; widthInPoints = width / scale; heightInPoints = height / scale; UIGraphicsBeginImageContextWithOptions(CGSizeMake(widthInPoints, heightInPoints), NO, scale); } else { // On iOS prior to 4, fall back to use UIGraphicsBeginImageContext widthInPoints = width; heightInPoints = height; UIGraphicsBeginImageContext(CGSizeMake(widthInPoints, heightInPoints)); } CGContextRef cgcontext = UIGraphicsGetCurrentContext(); // UIKit coordinate system is upside down to GL/Quartz coordinate system // Flip the CGImage by rendering it to the flipped bitmap context // The size of the destination area is measured in POINTS CGContextSetBlendMode(cgcontext, kCGBlendModeCopy); CGContextDrawImage(cgcontext, CGRectMake(0.0, 0.0, widthInPoints, heightInPoints), iref); // Retrieve the UIImage from the current context UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); // Clean up free(data); CFRelease(ref); CFRelease(colorspace); CGImageRelease(iref); return image;
}

3.使用

调用上述的方法就可以。

- (UIImage*)snapshot:(UIView*)eaglview

最新文章

  1. 使用google 语言 api 来实现整个网站的翻译
  2. java-并发-高级并发对象2
  3. 好用的Markdown编辑器一览
  4. Windows Store App JavaScript 开发:WinJS库控件
  5. SpringMVC使用@PathVariable,@RequestBody,@ResponseBody,@RequestParam,@InitBinder
  6. CEF3开发者系列之进程和线程
  7. uva729
  8. jQuery 实现Bootstrap Chart 图表
  9. dom 学习的开始~简单留言1
  10. ASP.NET MVC 4 中Jquery上传插件Uploadify简单使用-版本:3.2.1
  11. c#中的类型转换
  12. C# 函数覆盖总结学习
  13. c 统计字符串中字符出现的个数
  14. cocos2d-x环境的搭建之xcode-本人亲历成功搭建!
  15. Flappy bird源代码(略吊)
  16. X-005 FriendlyARM tiny4412 uboot移植之时钟初始化
  17. [Python]循环嵌套nested loop-练习题
  18. Nginx集群及代理的应用
  19. sql语句order by排序问题
  20. zw量化交易·实盘操作·系列培训班

热门文章

  1. 进程与multiprocessing模块
  2. 【CF1023C】Bracket Subsequence(模拟)
  3. 解决 unresolved external symbol 无法解析 _send@16(转)
  4. fork()函数的执行过程、孤儿进程和僵尸进程
  5. AppCode 中开发 Mac OS X 应用程序或共享库的经验小结
  6. WCF的学习之旅
  7. OS | Process
  8. wangzhi
  9. luogu P2746 [USACO5.3]校园网Network of Schools
  10. iOS -- SKScene类