2010-07-28 71 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 2012-09-21 08:35:28

回答

1

我已經完成了這個。

我用下面的CAEAGLLayer屬性:

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

,並在您- (的UIImage *)的getImage方法,而使用:

CGBitmapInfo bitmapInfo = kCGImageAlphaPremultipliedLast; 

如果需要的話您組合:方法可以被調整以通過使用以下來將兩個組合圖像保存到UIImage:

UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 

希望這有助於:)

0

你是如何創建OpenGL緩衝區的?具體而言,在你的EAGLView,時或您設置CAEAGLLayer的drawableProperties類屬性,它應該是這個樣子:

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

如果使用kEAGLColorFormatRGB565而不是kEAGLColorFormatRGBA8,你的幀緩衝區將不會被存儲alpha值,所以它永遠是不透明的。

如果您尚未設置此屬性,請嘗試設置它。

+0

我將drawableProperties的kEAGLDrawablePropertyRetainedBacking設置爲「YES」,因爲當我將它設置爲「NO」時,屏幕閃爍。 – Tony 2010-07-28 04:48:19

+0

這或多或少與alpha問題無關。它可能不應該閃爍,並且將其設置爲YES實際上可能會導致輕微的性能問題,但這是一個完全獨立的問題。我們最關心的是彩色格式。那工作? – Perrako 2010-07-28 05:45:06