2012-03-11 115 views
2

我正在使用OpenGL创建一款运行得非常好的iPhone游戏。我知道,当游戏按下主屏幕按钮后,游戏进入后台时,iOS会在应用程序返回前台时显示屏幕截图。从背景返回的iPhone屏幕截图全是白色

问题是,当您再次启动游戏(从背景中返回它)时,会显示一个白色图像,而不是显示游戏的最后一个屏幕。当然,这个游戏在后台运行的时候还是很好的。

我已经测试过这个问题只与模拟器没有真正的iPhone(模拟器v.5.0,iOS v.5.0)。

有没有人有这个问题和解决方案?我错过了什么?

更新:我发现一些Cocos2D用户have the same problem but without a solution。我不使用Cocos2D。

更新:我已检查iOS拍摄的快照是640x960白色jpg文件。所以也许问题是OpenGL-ES和游戏视图之间的某种连接。

+0

你是如何捕获屏幕截图的?请告诉我们。 – 2012-03-11 19:29:17

+0

Jeshua我没有截取屏幕截图,是iOS系统自动截屏并在应用程序再次激活时显示(以创建快速视觉加载体验)。截图显示全白,而不是游戏的最后一帧。 – jfcalvo 2012-03-11 19:33:49

回答

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); 
}