2012-07-02 12 views
5

मैं सोच रहा था कि कोई भी स्क्रीनशॉट कैसे ले सकता है इसका एक उदाहरण प्रदान कर सकता है जो OpenGL और UIKit तत्वों को मिश्रित करता है। जब से ऐप्पल ने UIGetScreenImage() निजी बनाया है, यह एक बहुत मुश्किल काम बन गया है क्योंकि ऐप्पल ने इसे बदलने के लिए उपयोग की जाने वाली दो सामान्य विधियों को केवल यूआईकिट या केवल ओपनजीएल पर कब्जा कर लिया है।प्रोग्रामेटिक रूप से ओपनजीएल और UIKit तत्वों का संयोजन स्क्रीनशॉट लें

This समान प्रश्न संदर्भ Apple's Technical Q&A QA1714, लेकिन क्यूए केवल वर्णन करता है कि कैमरे और UIKit से तत्वों को कैसे प्रबंधित किया जाए। यूआईकिट व्यू पदानुक्रम को एक छवि संदर्भ में प्रस्तुत करने के बारे में आप कैसे जाते हैं और उसके बाद अपने ओपनजीएल ईएस व्यू की छवि को इसी तरह के प्रश्न के उत्तर की तरह चित्रित करते हैं?

+0

वैकल्पिक रूप से बनाने के लिए, वहाँ UIGetScreenImage के लिए एक उपयुक्त प्रतिस्थापन कि इन बातों के दोनों संभाल होता है? – Austin

उत्तर

4

यह चाल करना चाहिए। मूल रूप से सीजी को सबकुछ प्रस्तुत करना और एक छवि बनाना जिससे आप जो कुछ भी कर सकते हैं।

// In Your UI View Controller 

- (UIImage *)createSavableImage:(UIImage *)plainGLImage 
{  
    UIImageView *glImage = [[UIImageView alloc] initWithImage:[myGlView drawGlToImage]]; 
    glImage.transform = CGAffineTransformMakeScale(1, -1); 

    UIGraphicsBeginImageContext(self.view.bounds.size); 

    //order of getting the context depends on what should be rendered first. 
    // this draws the UIKit on top of the gl image 
    [glImage.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    [someUIView.layer renderInContext:UIGraphicsGetCurrentContext()]; 

    UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    // Do something with resulting image 
    return finalImage; 
} 

// In Your GL View 

- (UIImage *)drawGlToImage 
{ 
    // Draw OpenGL data to an image context 

    UIGraphicsBeginImageContext(self.frame.size); 

    unsigned char buffer[320 * 480 * 4]; 

    CGContextRef aContext = UIGraphicsGetCurrentContext(); 

    glReadPixels(0, 0, 320, 480, GL_RGBA, GL_UNSIGNED_BYTE, &buffer); 

    CGDataProviderRef ref = CGDataProviderCreateWithData(NULL, &buffer, 320 * 480 * 4, NULL); 

    CGImageRef iref = CGImageCreate(320,480,8,32,320*4, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaLast, ref, NULL, true, kCGRenderingIntentDefault); 

    CGContextScaleCTM(aContext, 1.0, -1.0); 
    CGContextTranslateCTM(aContext, 0, -self.frame.size.height); 

    UIImage *im = [[UIImage alloc] initWithCGImage:iref]; 

    UIGraphicsEndImageContext(); 

    return im; 
} 

फिर, एक स्क्रीनशॉट

UIImage *glImage = [self drawGlToImage]; 
UIImage *screenshot = [self createSavableImage:glImage]; 
संबंधित मुद्दे