iPhone – saving OpenGL ES content to the Photo Album

http://www.bit-101.com/blog/?p=1861

C:

  1. -(UIImage *) glToUIImage {

  2.     NSInteger myDataLength = 320 * 480 * 4;

  3. // allocate array and read pixels into it.

  4.     GLubyte *buffer = (GLubyte *) malloc(myDataLength);

  5.     glReadPixels(0, 0, 320, 480, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

  6. // gl renders "upside down" so swap top to bottom into new array.

  7. // there's gotta be a better way, but this works.

  8.     GLubyte *buffer2 = (GLubyte *) malloc(myDataLength);

  9. for(int y = 0; y <480; y++)

  10. {

  11. for(int x = 0; x <320 * 4; x++)

  12. {

  13.             buffer2[(479 - y) * 320 * 4 + x] = buffer[y * 4 * 320 + x];

  14. }

  15. }

  16. // make data provider with data.

  17.     CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, myDataLength, NULL);

  18. // prep the ingredients

  19. int bitsPerComponent = 8;

  20. int bitsPerPixel = 32;

  21. int bytesPerRow = 4 * 320;

  22.     CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();

  23.     CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;

  24.     CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;

  25. // make the cgimage

  26.     CGImageRef imageRef = CGImageCreate(320, 480, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);

  27. // then make the uiimage from that

  28.     UIImage *myImage = [UIImage imageWithCGImage:imageRef];

  29. return myImage;

  30. }

  31. -(void)captureToPhotoAlbum {

  32.     UIImage *image = [self glToUIImage];

  33.     UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);

  34. }

你可能感兴趣的:(openGL ES)