【问题标题】:convert multiple EAGLView to UIImage将多个 EAGLView 转换为 UIImage
【发布时间】:2013-11-04 16:39:10
【问题描述】:

我有一个UITableView,每个单元格中有 3 个EAGLView,用户可以在其中绘制一些东西(在苹果GLPaint 之后,我只是使用该示例中的 PaintingView 类)

我正在尝试在按钮压力事件中将这 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;
}

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

【问题讨论】:

    标签: ios uiimage eaglview


    【解决方案1】:

    假设您是using retained backing 这些视图(否则glReadPixels() 在您呈现到屏幕后不起作用),我的猜测是每个视图都有不同的 OpenGL ES 上下文。 glReadPixels() 只会从当前活动的上下文中提取,因此是最后一个渲染的视图。

    在使用上述方法读取每个视图的上下文之前,您需要使用如下代码:

    [EAGLContext setCurrentContext:context];
    

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

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

    【讨论】:

    • 是的,那是我的问题!非常感谢 glCheckFramebufferStatusOES
    猜你喜欢
    • 2011-10-28
    • 2011-10-26
    • 2012-08-11
    • 1970-01-01
    • 2015-07-30
    • 2012-07-15
    • 2013-01-01
    • 2015-12-12
    • 2011-09-22
    相关资源
    最近更新 更多