2010-07-28 4 views
3

iPhone에서 OpenGL ES에 문제가 있습니다.iPhone에서 OpenGL ES 컨텍스트에서 투명한 이미지를 얻는 방법

나는 프로젝트에서 일하고 있는데,보기에 무언가를 그리고 나서 이미지를 저장한다. 또한 배경 이미지도 있습니다. 뷰와 배경 이미지에서 이미지를 결합했습니다.

뷰의 불투명 속성을 NO로 설정했지만 뷰에서 이미지가 여전히 불투명 해집니다. 그래서 저는 두 이미지를 결합 할 수 없었습니다. 나는 단지 앞면 이미지 만 볼 수 있었고 배경 이미지는 어디에도 없었다. 이 같은

CAEAGLLayer *EAGLLayer = (CAEAGLLayer *)self.layer; 
     EAGLLayer.opaque = NO; 

가져 오기 이미지 코드 :

- (UIImage *)GetImage 
{ 
    CGFloat Width = self.frame.size.width; 
    CGFloat Height = self.frame.size.height; 
    GLubyte *TmpBuffer = (GLubyte *)malloc(Width * Height * 4); 
    glReadPixels(0, 0, Width, Height, GL_RGBA, GL_UNSIGNED_BYTE, TmpBuffer); 
    GLubyte *Buffer = (GLubyte *)malloc(Width * Height * 4); 

    for(int y=0; y<Height; y++) { 
     for(int x=0; x<Width * 4; x++) { 
      Buffer[((NSInteger)Height - 1 - y) * (NSInteger)Width * 4 + x] = TmpBuffer[y * 4 * (NSInteger)Width + x]; 
     } 
    } 
    CGDataProviderRef Provider = CGDataProviderCreateWithData(NULL, Buffer, Width * Height * 4, NULL); 

    int BitsPerComponent = 8; 
    int BitsPerPixel = 32; 
    int BytesPerRow = 4 * 480; 
    CGColorSpaceRef ColorSpaceRef = CGColorSpaceCreateDeviceRGB(); 
    CGBitmapInfo BitmapInfo = kCGBitmapByteOrderDefault; 
    CGColorRenderingIntent RenderingIntent = kCGRenderingIntentDefault; 

    // Make the cgimage. 
    CGImageRef ImageRef = CGImageCreate(480, 320, 
             BitsPerComponent, 
             BitsPerPixel, 
             BytesPerRow, 
             ColorSpaceRef, 
             BitmapInfo, 
             Provider, 
             NULL, NO, 
             RenderingIntent); 

    return [UIImage imageWithCGImage:ImageRef]; 
} 

이 같은 코드를 결합 :

- (void)Combine:(UIImage *)Back 
{ 
    UIGraphicsBeginImageContext(self.frame.size); 

    CGContextRef aContext = UIGraphicsGetCurrentContext(); 

    CGContextSaveGState(aContext); 

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

    UIImage *Front = [self GetImage]; 

    CGContextDrawImage(aContext, 
         CGRectMake(0, 0, self.frame.size.width, self.frame.size.height), 
         Back.CGImage); 

    CGContextDrawImage(aContext, 
         CGRectMake(0, 0, self.frame.size.width, self.frame.size.height), 
         Front.CGImage); 

    CGContextRestoreGState(aContext); 

    UIImageWriteToSavedPhotosAlbum(UIGraphicsGetImageFromCurrentImageContext(), nil, nil, nil); 

    UIGraphicsEndImageContext(); 
} 

어떻게해야합니까? 어떤 제안이라도 기뻐할 것입니다. 미리 감사드립니다.

+0

그것이 iOS6의와 함께 일하고있다? – GameLoading

답변

1

나는 이것을 정확하게했다. 오히려 사용 (있는 UIImage *) 된 GetImage 방법 -

eaglLayer.opaque = NO;  
eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], kEAGLDrawablePropertyRetainedBacking,kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil]; 

하고 의 :

CGBitmapInfo bitmapInfo = kCGImageAlphaPremultipliedLast; 

당신의 를 필요한 경우 결합 :

나는 다음과 같은 CAEAGLLayer 속성을 사용 메서드를 사용하여 두 개의 결합 된 이미지를 UIImage에 저장하도록 조정할 수 있습니다.

,
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 

희망이 도움이 :)

0

어떻게 OpenGL 버퍼를 만드시겠습니까? 그래서,

eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil]; 

대신 kEAGLColorFormatRGBA8의 kEAGLColorFormatRGB565를 사용하는 경우

, 당신의 프레임 버퍼에 알파 값을 저장하지 않습니다 : 특히, 당신이 당신의 CAEAGLLayer의 drawableProperties 클래스의 속성을 설정하거나 EAGLView에, 그것은 같이 보일 것이다 항상 불투명합니다.

아직이 속성을 설정하지 않은 경우 설정을 시도하십시오.

+0

drawableProperties의 kEAGLDrawablePropertyRetainedBacking을 "YES"로 설정합니다. "NO"로 설정하면 화면이 깜박입니다. – Tony

+0

알파 문제와 관련이 없거나 적습니다. 아마도 깜박 거리지 않아야하고 예를 들어 실제로 성능 문제가 발생할 수 있지만 완전히 별개의 문제입니다. 우리는 주로 색 형식에 관심이있었습니다. 그 일 했니? – Perrako