6

I have an OpenGL ES 2 drawing app (iOS 4), so I'm retaining backing in my CAEAGLLayer rather than clearing on every frame:

eaglLayer.opaque = TRUE;
eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
                                        [NSNumber numberWithBool:TRUE], kEAGLDrawablePropertyRetainedBacking,
                                        kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat,
                                        nil];

I have a button for saving the work to photo album with the code below. Here's the issue. When it's done saving and I start drawing again, the whole buffer clears and my drawing begins from scratch on a blank screen. Is there a way to prevent this? I'd like to be able to continue drawing from the same state that I just saved.

I'm running the same code to save in OpenGL ES 1 and this issue doesn't occur there. Also, this issue is only visible on the iPhone device (3GS), not the simulator. Let me know if you have ideas. Thanks.

-(void)saveCurrentScreenToPhotoAlbum 
{
    CGRect rect = [[UIScreen mainScreen] bounds];
    int width = rect.size.width;
    int height = rect.size.height;

    NSInteger myDataLength = width * height * 4;
    GLubyte *buffer = (GLubyte *) malloc(myDataLength);
    GLubyte *buffer2 = (GLubyte *) malloc(myDataLength);
    glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

    for(int y = 0; y <height; y++) 
    {
        for(int x = 0; x <width * 4; x++) 
        {
            buffer2[(int)((height - 1 - y) * width * 4 + x)] = buffer[(int)(y * 4 * width + x)];
        }
    }
    free(buffer);

    CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, myDataLength, releaseData);
    int bitsPerComponent = 8;
    int bitsPerPixel = 32;
    int bytesPerRow = 4 * width;
    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
    CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
    CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
    CGImageRef imageRef = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);

    CGColorSpaceRelease(colorSpaceRef);
    CGDataProviderRelease(provider);     

    UIImage *image = [[UIImage alloc] initWithCGImage:imageRef];    // change this to manual alloc/init instead of autorelease
    CGImageRelease(imageRef);   

    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);   // add callback for finish saving
}

// callback for CGDataProviderCreateWithData
void releaseData(void *info, const void *data, size_t dataSize) 
{

    free((void*)data);   
}

// callback for UIImageWriteToSavedPhotosAlbum
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo 
{

    [image release];     
}
anna
  • 2,723
  • 4
  • 28
  • 37
  • 1
    `glReadPixels()` should not blank out your drawing area. If you save twice in a row (using `glReadPixels()` twice, but not doing any additional drawing), do you get two saved images or is one of them blank? Are you sure there isn't a code path where `glClear()` gets called before you draw? – Brad Larson Nov 29 '10 at 19:14
  • Agree with Brad. If you comment out the entire body of saveCurrentScreenToPhotoAlbum, then follow the same steps, does the screen clear? If not, half-split -- if you comment out the allocation & stuffing of buffer, and xfer of data to buffer2 (so buffer2 is garbage), does your screen clear? Etc. – Olie Mar 30 '11 at 15:45
  • Thank you for your comments. I think I have finally determined that the problem was due to my mistake! I was making another call while performing this function, which resulted in confused behavior by the buffer. How should I close this question? – anna Mar 30 '11 at 19:00
  • 2
    you can close the question by answering it and selecting that as the answer! – makdad Apr 23 '11 at 12:27

1 Answers1

1

Answer by Anna.

I was making another call while performing this function, which resulted in confused behavior by the buffer

Community
  • 1
  • 1
Viktor Apoyan
  • 10,655
  • 22
  • 85
  • 147