2012-06-14 1 views
1

가능한 중복은 : 사람이있는 UIImage 같은 EAGLContext에 저장되는 저장 할 수있는 방법을 알고 있다면 엑스 코드 스크린 샷 EAGLContext


How to get UIImage from EAGLView?

그래서 난 그냥 궁금 해서요. 내가 가진 다른 응용 프로그램에서

UIGraphicsBeginImageContext(CGSizeMake(768, 1024)); 
[self.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

및이 잘 작동하지만 분명히 EAGLContext는 .layer 속성을 가지고 있지 않습니다

내가 현재 사용하고 있습니다. 내가 UIView의에 캐스팅하려했지만, 그 - 당연히 - 작동하지 않습니다

UIView *newView = [[UIView alloc] init]; 
newView = (UIView *)context; 

내가보기 컨트롤러에 다른 UIView의에있는 UIView에있는 UIView에 EAGLContext 속성 (기술적으로 EAGLContext 드로잉하고, 하지만 OpenGLES 1을 사용해서는 안된다.)

아무에게도 이것에 대해 아는 사람이 없다면, 그저 내가 완전히 불가능한 나무를 짖고있다하더라도 알려 주시기 바랍니다! 매트

답변

3

며칠 후 나는 마침내에 대한 작업 솔루션을 얻었다. EAGLView에서 UIImage를 생성하는 Apple에서 제공하는 코드가 있습니다. 그런 다음 UIKit이 거꾸로되어 있기 때문에 이미지를 수직으로 뒤집기 만하면됩니다. 이 메서드를 발견 한 문서에 대한 링크가 더 이상 존재하지 않습니다.

방법 EAGLView을 캡처 :

-(UIImage *)drawableToCGImage 
{ 
    GLint backingWidth2, backingHeight2; 
    //Bind the color renderbuffer used to render the OpenGL ES view 
    // If your application only creates a single color renderbuffer which is already bound at this point, 
    // this call is redundant, but it is needed if you're dealing with multiple renderbuffers. 
    // Note, replace "_colorRenderbuffer" with the actual name of the renderbuffer object defined in your class. 
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer); 

    // Get the size of the backing CAEAGLLayer 
    glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth2); 
    glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight2); 

    NSInteger x = 0, y = 0, width2 = backingWidth2, height2 = backingHeight2; 
    NSInteger dataLength = width2 * height2 * 4; 
    GLubyte *data = (GLubyte*)malloc(dataLength * sizeof(GLubyte)); 

    // Read pixel data from the framebuffer 
    glPixelStorei(GL_PACK_ALIGNMENT, 4); 
    glReadPixels(x, y, width2, height2, GL_RGBA, GL_UNSIGNED_BYTE, data); 

    // Create a CGImage with the pixel data 
    // If your OpenGL ES content is opaque, use kCGImageAlphaNoneSkipLast to ignore the alpha channel 
    // otherwise, use kCGImageAlphaPremultipliedLast 
    CGDataProviderRef ref = CGDataProviderCreateWithData(NULL, data, dataLength, NULL); 
    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB(); 
    CGImageRef iref = CGImageCreate(width2, height2, 8, 32, width2 * 4, colorspace, kCGBitmapByteOrder32Big | kCGImageAlphaPremultipliedLast, 
            ref, NULL, true, kCGRenderingIntentDefault); 

    // OpenGL ES measures data in PIXELS 
    // Create a graphics context with the target size measured in POINTS 
    NSInteger widthInPoints, heightInPoints; 
    if (NULL != UIGraphicsBeginImageContextWithOptions) { 
     // On iOS 4 and later, use UIGraphicsBeginImageContextWithOptions to take the scale into consideration 
     // Set the scale parameter to your OpenGL ES view's contentScaleFactor 
     // so that you get a high-resolution snapshot when its value is greater than 1.0 
     CGFloat scale = self.contentScaleFactor; 
     widthInPoints = width2/scale; 
     heightInPoints = height2/scale; 
     UIGraphicsBeginImageContextWithOptions(CGSizeMake(widthInPoints, heightInPoints), NO, scale); 
    } 
    else { 
     // On iOS prior to 4, fall back to use UIGraphicsBeginImageContext 
     widthInPoints = width2; 
     heightInPoints = height2; 
     UIGraphicsBeginImageContext(CGSizeMake(widthInPoints, heightInPoints)); 
    } 

    CGContextRef cgcontext = UIGraphicsGetCurrentContext(); 

    // UIKit coordinate system is upside down to GL/Quartz coordinate system 
    // Flip the CGImage by rendering it to the flipped bitmap context 
    // The size of the destination area is measured in POINTS 
    CGContextSetBlendMode(cgcontext, kCGBlendModeCopy); 
    CGContextDrawImage(cgcontext, CGRectMake(0.0, 0.0, widthInPoints, heightInPoints), iref); 

    // Retrieve the UIImage from the current context 
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 

    UIGraphicsEndImageContext(); 

    // Clean up 
    free(data); 
    CFRelease(ref); 
    CFRelease(colorspace); 
    CGImageRelease(iref); 

    return image; 
} 

방법을 수직 방향으로 이미지를 뒤집을 :

- (UIImage *)flipImageVertically:(UIImage *)originalImage 
{ 
    UIImageView *tempImageView = [[UIImageView alloc] initWithImage:originalImage]; 
    UIGraphicsBeginImageContext(tempImageView.frame.size); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGAffineTransform flipVertical = CGAffineTransformMake(
                  1, 0, 0, -1, 0, tempImageView.frame.size.height 
                  ); 
    CGContextConcatCTM(context, flipVertical); 

    [tempImageView.layer renderInContext:context]; 

    UIImage *flippedImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    //[tempImageView release]; 

    return flippedImage; 
} 
+0

6으로 OS를 갖는 장치에서 작업이 하나의인가? – Dee

+0

해당 프로젝트에서 더 이상 작업하지 않으므로 테스트하지 않았습니다. 왜 작동하지 않는지 상상할 수는 없지만, iOS 6를 사용하여 gl을 변경했다고 생각하지 않습니다. –