我有一个 OpenGL ES 2 绘图应用程序(iOS 4),所以我在我的 CAEAGLLayer 中保留支持,而不是在每一帧上清除:
eaglLayer.opaque = TRUE;
eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:TRUE], kEAGLDrawablePropertyRetainedBacking,
kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat,
nil];
我有一个按钮,可以使用下面的代码将作品保存到相册。这就是问题所在。当它完成保存并且我再次开始绘图时,整个缓冲区被清除,我的绘图在空白屏幕上从头开始。有没有办法防止这种情况?我希望能够从我刚刚保存的相同状态继续绘图。
我正在运行相同的代码以保存在 OpenGL ES 1 中,并且该问题不会在那里发生。此外,此问题仅在 iPhone 设备 (3GS) 上可见,在模拟器上不可见。让我知道你是否有想法。谢谢。
-(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];
}