ios - GLKView 快照方法 : null return val, 出错

标签 ios opengl-es uiimage

我不知道如何使用 GLKView:snapshot 方法。

我正在使用 GLKView 渲染一些 OpenGL 内容。一切正常;好像我已经正确设置了所有内容。

但是,当我尝试创建快照时,它失败了:我得到一个空返回值,以及以下日志消息:

错误:CGImageCreate:图像大小无效:0 x 0。

这似乎意味着 View 本身由于某种原因无效,但事实并非如此——除此之外一切正常。

我查看了一些代码示例,但没有做任何不同的事情。

所以...有人以前见过这个吗?想法?

最佳答案

上面的问题一直没弄明白;但是,我找到了一个很好的解决方法。我发现这个 block 只读取渲染缓冲区并将其保存到 UIImage。问题解决了!

- (UIImage*)snapshotRenderBuffer {

    // Bind the color renderbuffer used to render the OpenGL ES view
    // If your application only creates a single color renderbuffer which is already bound at this point, 
    // this call is redundant, but it is needed if you're dealing with multiple renderbuffers.
    // Note, replace "_colorRenderbuffer" with the actual name of the renderbuffer object defined in your class.
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);

    NSInteger dataLength = backingWidth * backingHeight * 4;
    GLubyte *data = (GLubyte*)malloc(dataLength * sizeof(GLubyte));

    // Read pixel data from the framebuffer
    glPixelStorei(GL_PACK_ALIGNMENT, 4);
    glReadPixels(0.0f, 0.0f, backingWidth, backingHeight, GL_RGBA, GL_UNSIGNED_BYTE, data);

    // Create a CGImage with the pixel data
    // If your OpenGL ES content is opaque, use kCGImageAlphaNoneSkipLast to ignore the alpha channel
    // otherwise, use kCGImageAlphaPremultipliedLast
    CGDataProviderRef ref = CGDataProviderCreateWithData(NULL, data, dataLength, NULL);
    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
    CGImageRef iref = CGImageCreate(
                                    backingWidth, backingHeight, 8, 32, backingWidth * 4, colorspace, 
                                    kCGBitmapByteOrder32Big | kCGImageAlphaNoneSkipLast,
                                    ref, NULL, true, kCGRenderingIntentDefault);

    // (sayeth abd)
    // This creates a context with the device pixel dimensions -- not points. 
    // To be compatible with all devices, you're meant to keep everything as points and a scale factor;  but,
    // this gives us a scaled down image for purposes of saving.  So, keep everything in device resolution,
    // and worry about it later...
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(backingWidth, backingHeight), NO, 0.0f);
    CGContextRef cgcontext = UIGraphicsGetCurrentContext();
    CGContextSetBlendMode(cgcontext, kCGBlendModeCopy);
    CGContextDrawImage(cgcontext, CGRectMake(0.0, 0.0, backingWidth, backingHeight), iref);

    // Retrieve the UIImage from the current context
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    // Clean up
    free(data);

    return image;
}

关于ios - GLKView 快照方法 : null return val, 出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11837432/

相关文章:

ios - 链接到 iOS 10 锁屏小部件文档?

ios - 自定义 barbuttonItem 不显示

android - 复制岛只有一组图像,为什么?

ios - glTexImage2D 完成上传后如何得到通知?

swift - 调整图像大小不正确?

javascript - iOS 上忽略的 jQuery 单击事件

ios - 如何附加对象 Realmswift 的列表属性

iphone - 基于 iOS 的 OpenGL ES 编程

swift - 将 [[UIColor]] 双数组转换为 UIImage

ios - 如何在 UIImage 上绘制时修复此内存泄漏?