참고 : http://stackoverflow.com/questions/5062978/how-can-i-dump-opengl-renderbuffer-to-png-or-jpg-image


-(UIImage *)dumpImage

{

    GLubyte *buffer = (GLubyte *) malloc(backingWidth * backingHeight * 4);

    GLubyte *buffer2 = (GLubyte *) malloc(backingWidth * backingHeight * 4);


    GLvoid *pixel_data = nil;

    glReadPixels(0, 0, backingWidth, backingHeight, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid *)buffer);


    /* make upside down */


    for (int y=0; y<backingHeight; y++) {

        for (int x=0; x<backingWidth*4; x++) {

            buffer2[y * 4 * backingWidth + x] = buffer[(backingHeight - y - 1) * backingWidth * 4 + x];

        }

    }


    // make data provider from buffer

    CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, backingWidth * backingHeight * 4, freeImageData);


    // set up for CGImage creation

    int bitsPerComponent = 8;

    int bitsPerPixel = 32;

    int bytesPerRow = 4 * backingWidth;

    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();

    CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;

    // Use this to retain alpha

    //CGBitmapInfo bitmapInfo = kCGImageAlphaPremultipliedLast;

    CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;

    CGImageRef imageRef = CGImageCreate(backingWidth, backingHeight, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);


    // make UIImage from CGImage

    UIImage *newUIImage = [UIImage imageWithCGImage:imageRef];


    return newUIImage;

}


void freeImageData(void *info, const void *data, size_t size) 

{

    //printf("freeImageData called");

    free((void*)data);

}

+ Recent posts