2

I want to call CGBitmapContextCreate with texture->data to create a CGContextRef, and create a CGImageRef by CGBitmapContextCreateImage(context).

However, the image created is not as expected :(

The one created from CGBitmapContextCreateImage:

enter image description here

The actual one (slight different since I take it with another camera):

enter image description here

Codes: (texture.bytesPerPixel = 2)

    CGContextRef context = CGBitmapContextCreate(texture.data, 512, 512, 5, 512 * texture.bytesPerPixel, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaNoneSkipFirst);
    CGImageRef cg_img = CGBitmapContextCreateImage(context);
    UIImage* ui_img = [UIImage imageWithCGImage: cg_img];
    UIImageWriteToSavedPhotosAlbum(ui_img, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);

Complete Code:

http://ihome.ust.hk/~tm_lksac/OpenGLSprite.m

The application usually calls - (void)drawSelfIfNeeded:(BOOL)needed to update the texture in the application. But I want to take a "screenshoot" of the texture and save it as UIImage as further image processing.

Rishil Patel
  • 1,977
  • 3
  • 14
  • 30
LKS
  • 673
  • 2
  • 10
  • 21
  • 1
    Does your code work fine if you use the 4-byte RGBA instead of the 2-byte GL_UNSIGNED_SHORT_5_6_5? As described in [this answer](http://stackoverflow.com/a/4694198/19679), you will need to do a little conversion to bring in 5,6,5 format data into a Core Graphics bitmap context. Also, you're leaking a CGColorSpaceRef in the above code. – Brad Larson Mar 31 '12 at 21:16
  • Brad, thanks for the link and it really helps. I can use the glReadPixels to get meaningful images. Now, working on details to get the right dimension and orientation. – LKS Apr 09 '12 at 09:15

0 Answers0