========================================================== =============================== Original blog, reprinted please declare the source of Electronic coffee (original id blue rock) ========================================================== ================================
The previous blog introduced how to perform common operations: IOS screen --- normal
As mentioned earlier, normal screenshots cannot capture the view rendered by OpenGL. It's like when we remotely log on to another Windows computer, we can see the desktop of the other party, but we cannot see the video of the other party.
For this type of view, we need to read all its pixels into the array through glreadpixels, and then create a uiimage through these pixel data. The detailed code is as follows.
In our OpenGL view, implement the following method:
//In Your GL View
//convert opengl data to UIImage-(UIImage *) glToUIImage { CGSize viewSize=self.frame.size; NSInteger myDataLength = viewSize.width * viewSize.height * 4; // allocate array and read pixels into it. GLubyte *buffer = (GLubyte *) malloc(myDataLength); glReadPixels(0, 0, viewSize.width, viewSize.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 < viewSize.height; y++) { for(int x = 0; x < viewSize.width* 4; x++) { buffer2[(int)((viewSize.height-1 - y) * viewSize.width * 4 + x)] = buffer[(int)(y * 4 * viewSize.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 * viewSize.width; CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB(); CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault; CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault; // make the cgimage CGImageRef imageRef = CGImageCreate(viewSize.width , viewSize.height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent); // then make the uiimage from that UIImage *myImage = [UIImage imageWithCGImage:imageRef]; return myImage;}
In addition, when implementing this code, the buffer obtained by the Code is still empty.
glReadPixels(0, 0, viewSize.width, viewSize.height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
Google explained that this is a problem with ios6. You need to set the attribute keagldrawablepropertyretainedbacking to Yes during caeagllayer initialization.
As follows:
CAEAGLLayer *eaglLayer = (CAEAGLLayer *) self.layer;eaglLayer.drawableProperties = @{ kEAGLDrawablePropertyRetainedBacking: [NSNumber numberWithBool:YES], kEAGLDrawablePropertyColorFormat: kEAGLColorFormatRGBA8};
Ref:
Download an image and save it as PNG or JPEG in iPhone SDK export objectgraph blog
IPhone-why do I get 'no-renderincontext: Method Found 'Warning? -Stack Overflow
IPhone-programmatically take a screenshot combining OpenGL and uikit elements-Stack Overflow
IPhone-saving OpenGL ES content to the photo album | b-101
OpenGL ES-why is glreadpixels () Failing in this code in IOS 6.0? -Stack Overflow