2012-06-23 7 views
5

मैं GLView में पिक्सल का स्क्रीनशॉट लेने के लिए निम्न कोड का उपयोग कर रहा हूं। समस्या यह है कि यह पूरी तरह से काला UIImage देता है। इस कोड को LineDrawer.m में बुलाया जा रहा है जो GLView कोड का दिल है - इसलिए इसे दाएं .m फ़ाइल से बुलाया जा रहा है। मैं वास्तविक स्क्रीनशॉट कैसे सहेज सकता हूं, न कि काले छवि?यह जीएलवीव स्क्रीनशॉट कोड एक रिक्त/काला यूआईएममेज क्यों लौटा रहा है?

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

आप अपने दो बफ़र्स मुक्त और जारी 'provider',' colorSpaceRef', और 'imageRef' की जरूरत है। इसके अलावा आप कहीं भी नकारात्मक 'stride' मान की आपूर्ति करके ऊर्ध्वाधर फ्लिप कर सकते हैं। – jhabbott

+0

मैं यह कैसे कर सकता हूं? क्षमा करें, लेकिन मैं ओपनजीएल के साथ बहुत अनुभवी नहीं हूं और यह एक ड्रॉप-इन लाइब्रेरी है! मैं वास्तव में एक उदाहरण की सराहना करता हूं :-) –

+0

मेरी टिप्पणी आपके प्रश्न का उत्तर नहीं है, मैं आपकी याददाश्त लीक को इंगित कर रहा हूं। आपको सामान्य रूप से कोरफाउंडेशन और ऑब्जेक्टिव-सी में संदर्भ गिनती के बारे में पढ़ना चाहिए। मॉलोक का उपयोग करते समय आपको सी में स्मृति प्रबंधन के बारे में भी जागरूक होना चाहिए। – jhabbott

उत्तर

0

मैं गौरैया फ्रेमवर्क में कुछ इसी तरह एक समय पहले करना था, तो आप भागों आप इस मंच उत्तर में कोड से बाहर की जरूरत को खींचने के लिए सक्षम होना चाहिए: http://forum.sparrow-framework.org/topic/spdisplayobjectscreenshot

संपादित करें: इसके अलावा इस पोस्ट http://forum.sparrow-framework.org/topic/taking-screenshots

0

बदलें अपनी drawable गुण

eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:

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

हां में kEAGLDrawablePropertyRetainedBacking

संबंधित मुद्दे