2012-07-02 27 views
5

我想知道是否有人可以提供一個如何截取混合了OpenGL和UIKit元素的截圖。自從蘋果公司製造UIGetScreenImage()以來,這已經成爲一項相當艱鉅的任務,因爲Apple用來取代它的兩種常用方法僅捕獲UIKit或僅捕獲OpenGL。以編程方式將OpenGL和UIKit元素相結合的屏幕截圖

This類似的問題參考Apple's Technical Q&A QA1714,但QA只描述瞭如何處理來自相機和UIKit的元素。如何將UIKit視圖層次結構渲染爲圖像上下文,然後在其上繪製OpenGL ES視圖的圖像,就像類似問題的答案所示的那樣?

+0

,有沒有合適的替代UIGetScreenImage會同時處理這些事情? – Austin

回答

4

這應該可以做到。基本上把所有東西都渲染到CG並創建一個圖像,你可以做任何事情。

// 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]; 
相關問題