2012-07-02 30 views
5

Tôi đã tự hỏi liệu có ai có thể cung cấp ví dụ về cách chụp ảnh màn hình kết hợp các phần tử OpenGL và UIKit không. Kể từ khi Apple thực hiện UIGetScreenImage() riêng việc này đã trở thành một nhiệm vụ khá khó khăn vì hai phương pháp phổ biến mà Apple sử dụng để thay thế nó chỉ chụp UIKit hoặc chỉ OpenGL.Lập trình chụp ảnh màn hình kết hợp các yếu tố OpenGL và UIKit

This tham chiếu câu hỏi tương tự Apple's Technical Q&A QA1714, nhưng QA chỉ mô tả cách xử lý các phần tử từ máy ảnh và UIKit. Làm thế nào để bạn đi về rendering phân cấp xem UIKit vào một bối cảnh hình ảnh và sau đó vẽ hình ảnh của bạn xem OpenGL ES trên đầu trang của nó giống như câu trả lời cho câu hỏi tương tự cho thấy?

+0

cách khác, là có một sự thay thế thích hợp cho UIGetScreenImage rằng sẽ xử lý cả những điều này? – Austin

Trả lời

4

Điều này cần thực hiện thủ thuật. Về cơ bản hiển thị mọi thứ cho CG và tạo ra một hình ảnh bạn có thể làm bất cứ điều gì.

// 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; 
} 

Sau đó, để tạo ra một ảnh chụp màn hình

UIImage *glImage = [self drawGlToImage]; 
UIImage *screenshot = [self createSavableImage:glImage]; 
Các vấn đề liên quan