2012-06-23 13 views
5

Korzystam z następującego kodu, aby zrobić zrzut ekranu pikseli w GLView. Problem polega na tym, że zwraca on całkowicie czarny UIImage. Ten kod jest wywoływany w LineDrawer.m, który jest sercem kodu GLView - dlatego wywoływany jest z właściwego pliku .m. Jak mogę zapisać rzeczywisty zrzut ekranu, a nie czarny obraz?Dlaczego ten kod wyświetlający GLView zwraca pustą/czarną UIImage?

- (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

Trzeba uwolnić dwa bufory i zwolnić ',' colorSpaceRef' provider' i 'imageRef'. Również prawdopodobnie możesz wykonać pionową klapkę, podając gdzieś ujemną wartość kroku. – jhabbott

+0

Jak mogę to zrobić? Przepraszam, ale nie mam zbyt dużego doświadczenia z OpenGL i jest to biblioteka z wtyczkami! Byłbym wdzięczny za przykład :-) –

+0

Mój komentarz nie jest odpowiedzią na twoje pytanie, wskazuję na wycieki pamięci. Powinieneś przeczytać o liczeniu referencji w CoreFoundation i Objective-C w ogóle. Musisz również pamiętać o zarządzaniu pamięcią w C przy użyciu malloc. – jhabbott

Odpowiedz

0

zmienić odkształcalne właściwości

eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:

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

kEAGLDrawablePropertyRetainedBacking YES

Powiązane problemy