Previously wrote a regular screen: http://blog.csdn.net/hitwhylz/article/details/17189351
But found that this method is useless for OpenGL. The data obtained is empty.
So here are the next OpenGL ES screens.
1. Initialize.
Caeagllayer *eagllayer = (Caeagllayer *) Self.layer; Eagllayer.drawableproperties = @{ keagldrawablepropertyretainedbacking: [NSNumber Numberwithbool:yes], Keagldrawablepropertycolorformat:keaglcolorformatrgba8
2. Declare the method.
-(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 the this point,//The This is the redundant, but it's needed if you ' re dealing w ith 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_wid Th_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 are opaque, use Kcgimagealphanoneskiplast to IG Nore the alpha channel//Otherwise, use kcgimagealphapremultipliedlast cgdataproviderref ref = Cgdataprovid Ercreatewithdata (null, data, datalength, NULL); Cgcolorspaceref colorspace = Cgcolorspacecreatedevicergb (); Cgimageref iref = cgimagecreate (width, height, 8, +, Width * 4, colorspace, Kcgbitmapbyteorder32big | kcgimagealphapremul Tipliedlast, ref, NULL, True, Kcgrenderingintentdef Ault); OpenGL ES measures data in PIXELS//Create A graphics context with the target size measured in POINTS NS Integer widthinpoints, heightinpoints; if (NULL! = UigraphicsbeginImagecontextwithoptions) {//on IOS 4 and later, use Uigraphicsbeginimagecontextwithoptions to take the SC Ale into consideration//Set the scale parameter to your OpenGL ES view ' s Contentscalefactor So, you get a high-resolution snapshot when it 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-Uigraphicsbeginimagecontext Widthinpo INTs = width; heightinpoints = height; Uigraphicsbeginimagecontext (Cgsizemake (widthinpoints, heightinpoints)); } cgcontextref Cgcontext = Uigraphicsgetcurrentcontext (); UIKit coordinate system is upside down toGl/quartz coordinate system//Flip the cgimage by rendering it to the flipped bitmap context//the size of The destination area was 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. Use
Call the method above to do so.
-(uiimage*) Snapshot: (uiview*) Eaglview
iOS Development-OpenGL es screen