2013-11-04 77 views
1

我有一个每个单元格,用户能够得出的东西(以下苹果GLPaint,我只是用来自例如PaintingView类)转换多个EAGLView到的UIImage

我里面“M试图转换这3 EAGLView按钮压力事件到3个不同的UIImage,但所产生的UIImages总是等于编辑

此最后EAGLView是我正在使用的代码(从here复制):

- (UIImage*)saveImageFromGLView:(UIView *)glView withName:(NSString *)name{ 

if(glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES) 
{ 
    /// This IS being activated with code 0 
    NSLog(@"failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES)); 
} 

int s = 1; 
UIScreen* screen = [ UIScreen mainScreen ]; 
if ([ screen respondsToSelector:@selector(scale) ]) 
    s = (int) [ screen scale ]; 

const int w = self.frame.size.width; 
const int h = self.frame.size.height; 
const NSInteger myDataLength = w * h * 4 * s * s; 
// allocate array and read pixels into it. 
GLubyte *buffer = (GLubyte *) malloc(myDataLength); 
glReadPixels(0, 0, w*s, h*s, GL_RGBA, GL_UNSIGNED_BYTE, buffer); 
// gl renders "upside down" so swap top to bottom into new array. 
// there's gotta be a better way, but this works. 
GLubyte *buffer2 = (GLubyte *) malloc(myDataLength); 
for(int y = 0; y < h*s; y++) 
{ 
    memcpy(buffer2 + (h*s - 1 - y) * w * 4 * s, buffer + (y * 4 * w * s), w * 4 * s); 
} 
free(buffer); // work with the flipped buffer, so get rid of the original one. 

// make data provider with data. 
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, myDataLength, NULL); 
// prep the ingredients 
int bitsPerComponent = 8; 
int bitsPerPixel = 32; 
int bytesPerRow = 4 * w * s; 
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB(); 
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault; 
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault; 
// make the cgimage 
CGImageRef imageRef = CGImageCreate(w*s, h*s, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent); 
// then make the uiimage from that 
UIImage *myImage = [ UIImage imageWithCGImage:imageRef scale:s orientation:UIImageOrientationUp ]; 

// Create path. 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:name]; 

// Save image. 
[UIImagePNGRepresentation(myImage) writeToFile:filePath atomically:YES]; 

CGImageRelease(imageRef); 
CGDataProviderRelease(provider); 
CGColorSpaceRelease(colorSpaceRef); 
free(buffer2); 

return myImage; 
} 

我认为这是一个上下文问题,我怎么能获得三个不同的图像?

回答

1

假设你是这些视图的using retained backing(否则glReadPixels()在你进入屏幕后不起作用),我的猜测是你对每个视图都有不同的OpenGL ES上下文。 glReadPixels()只会从当前活动的上下文中拉出,因此也会从最后一次呈现的视图中拉出。

之前,你从每个视图在上述方法的上下文看,你要使用如下代码:

[EAGLContext setCurrentContext:context]; 

其中上下文是从你的OpenGL ES渲染UIView的拉动。您可能希望将该上下文设置为UIView子类的只读属性,然后更改上述方法以接受该子类。然后,您可以在使用glReadPixels()之前切换到适当的上下文。

作为一个问题,你不应该在上面使用glCheckFramebufferStatusOES()。这只是真的属于你的帧缓冲区创建代码,并没有真正的目的在你的上述方法。

+0

是的,那是我的问题!非常感谢,也为glCheckFramebufferStatusOES – Janky