我正在使用OpenGL创建一款效果非常好的iPhone游戏。我知道当按下主页按钮后游戏进入后台时,iOS会在应用程序返回前景时显示要显示的屏幕截图。
问题在于,当您再次启动游戏(从背景返回)时,会显示白色图像而不是游戏的最后一个屏幕。当然,游戏在目前的工作状态下正常运行。
我已经使用不使用真实iPhone的模拟器(Simulator v.5.0,iOS v.5.0)测试了这个问题。
有没有人这个问题和解决方案?我错过了什么?。
更新:我发现有些Cocos2D用户have the same problem but without a solution。我不使用Cocos2D。
更新:我已经检查过iOS拍摄的快照是640x960白色jpg文件。所以问题可能是OpenGL-ES与游戏视图之间的某种连接。
答案 0 :(得分:1)
如果您的应用程序在OpenGL中,那么您可以使用glReadPixels读取图像屏幕。
- (UIImage*) getGLScreenshot {
NSInteger myDataLength = 320 * 480 * 4;
// allocate array and read pixels into it.
GLubyte *buffer = (GLubyte *) malloc(myDataLength);
glReadPixels(0, 0, 320, 480, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
// gl renders "upside down" so swap top to bottom into new array.
// there's gotta be a better way, but this works.
GLubyte *buffer2 = (GLubyte *) malloc(myDataLength);
for(int y = 0; y <480; y++)
{
for(int x = 0; x <320 * 4; x++)
{
buffer2[(479 - y) * 320 * 4 + x] = buffer[y * 4 * 320 + x];
}
}
// make data provider with data.
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, myDataLength, NULL);
// prep the ingredients
int bitsPerComponent = 8;
int bitsPerPixel = 32;
int bytesPerRow = 4 * 320;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
// make the cgimage
CGImageRef imageRef = CGImageCreate(320, 480, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
// then make the uiimage from that
UIImage *myImage = [UIImage imageWithCGImage:imageRef];
return myImage;
}
- (void)saveGLScreenshotToPhotosAlbum {
UIImageWriteToSavedPhotosAlbum([self getGLScreenshot], nil, nil, nil);
}