iphone - GLPaint 保存图像

标签 iphone objective-c opengl-es quartz-graphics

我正在尝试在 iPhone 上开发一个复杂的绘画应用程序。我目前正在使用 Quartz 绘图(例如 CGContext...)。不幸的是,Quartz 开销对于我正在做的绘图类型来说太慢了,我正在使用 GLPaint 示例作为引用点移植到 OpenGL 调用。

有没有办法从 EAGLview 类中获取 UIImage/CGImage(相当于 Quartz 的 UIGraphicsGetImageFromCurrentImageContext)?基本上我需要保存 GLPaint 应用程序绘制的图片。

最佳答案

-(UIImage *) saveImageFromGLView
{
    NSInteger myDataLength = 320 * 480 * 4;
    // allocate array and read pixels into it.
    GLubyte *buffer = (GLubyte *) malloc(myDataLength);
    glReadPixels(0, 0, 320, 480, 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 <480; y++)
    {
        for(int x = 0; x <320 * 4; x++)
        {
            buffer2[(479 - y) * 320 * 4 + x] = buffer[y * 4 * 320 + x];
        }
    }
    // make data provider with data.
    CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, myDataLength, NULL);
    // prep the ingredients
    int bitsPerComponent = 8;
    int bitsPerPixel = 32;
    int bytesPerRow = 4 * 320;
    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
    CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
    CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
    // make the cgimage
    CGImageRef imageRef = CGImageCreate(320, 480, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
    // then make the uiimage from that
    UIImage *myImage = [UIImage imageWithCGImage:imageRef];
    return myImage;
}

关于iphone - GLPaint 保存图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/946700/

相关文章:

iphone - openGL 中的 3d 游戏开发人员

android - 如何拦截 Android OpenGL ES 中的触摸屏事件?

ios - 在 iOS 中渲染水彩画

iPhone 应用程序被拒绝。 9.4 : Video streaming over cellular network (etc)

ios - 当我通过我的应用程序打开 App Store 时,在 App Store 中显示打开应用程序而不是更新应用程序

iphone - 一个类(class)的 setter/getter ?

iphone - 检测自定义按钮的 UIAppearance 代理中的更改

ios - UiTextView拖动选定的文本

ios - 如何在uibutton之类的消息应用程序上使用角标(Badge)?

ios - 更新标题时如何使按钮停止闪烁