2012-06-23 8 views
5

Estoy usando el siguiente código para tomar una captura de pantalla de los píxeles en un GLView. El problema es que devuelve un UIImage completamente negro. Se llama a este código en LineDrawer.m, que es el corazón del código GLView, por lo que se lo está llamando desde el archivo .m correcto. ¿Cómo puedo guardar la captura de pantalla real y no una imagen en negro?¿Por qué este código de captura de pantalla GLView devuelve un UIImage en blanco/negro?

- (UIImage*) getGLScreenshot { 

NSLog(@"1"); 

float scale = 0.0; 
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) 
{ 
    // scale value should be 1.0 on 3G and 3GS, and 2.0 on iPhone 4. 
    scale = [[UIScreen mainScreen] scale]; 
} 

// these are swapped since the screen is rotatey 
float h = 768 * scale; 
float w = 924 * scale; 

NSInteger myDataLength = w * h * 4; 

// allocate array and read pixels into it. 
GLubyte *buffer = (GLubyte *) malloc(myDataLength); 
glReadPixels(0, 0, w, h, 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 <h; y++) 
{ 
    for(int x = 0; x <w * 4; x++) 
    { 
     buffer2[(((int)h-1) - y) * (int)w * 4 + x] = buffer[y * 4 * (int)w + 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 * w; 
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB(); 
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault; 
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault; 

// make the cgimage 
CGImageRef imageRef = CGImageCreate(w, h, 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); 
} 
+1

Es necesario para liberar a sus dos tampones y suelte el prestador de '' colorSpaceRef', y 'imageRef'. También es probable que pueda hacer la inversión vertical al proporcionar un valor de "zancada" negativo en alguna parte. – jhabbott

+0

¿Cómo puedo hacer esto? Lo siento, pero no tengo demasiada experiencia con OpenGL y esta es una biblioteca gratuita. Realmente agradecería un ejemplo :-) –

+0

Mi comentario no es una respuesta a su pregunta, estoy señalando sus pérdidas de memoria. Debería leer sobre recuento de referencias en CoreFoundation y Objective-C en general. También debe tener en cuenta la administración de la memoria en C cuando usa malloc. – jhabbott

Respuesta

0

cambiar sus propiedades dibujable

eaglLayer.drawableProperties = [dictionaryWithObjectsAndKeys NSDictionary:

  [NSNumber numberWithBool:YES], 
       kEAGLDrawablePropertyRetainedBacking, 
       kEAGLColorFormatRGB565, 
       kEAGLDrawablePropertyColorFormat, nil]; 

kEAGLDrawablePropertyRetainedBacking a SÍ

Cuestiones relacionadas