我在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();
}
我該怎麼辦?任何建議都會令人高興。提前致謝。
是它與iOS6的工作? – GameLoading 2012-09-21 08:35:28