【问题标题】:When glReadPixels can be used?什么时候可以使用 glReadPixels?
【发布时间】:2012-07-09 21:47:58
【问题描述】:

我想知道GLReadPixels函数的使用。/ 它是如何读取像素的? 它是在读取GLKView 像素还是UIView 像素或主屏幕上在 glreadFunction 提供的范围内的任何内容。 或者只有在我们使用GLKView时才能使用它??

请澄清我的疑问。

【问题讨论】:

    标签: iphone xcode ipad glreadpixels


    【解决方案1】:

    它从当前的 OpenGL (ES) 帧缓冲区中读取像素。它不能用于从UIView 读取像素,但它可以用于从GLKView 读取像素,因为它由帧缓冲区支持(但是,您只能在它最活跃的帧缓冲区时读取它的数据可能是在绘图时)。但是,如果您想要的只是GLKView 的屏幕截图,您可以使用其内置的snapshot 方法来获取带有其内容的UIImage

    【讨论】:

    • 感谢您的解释。还有一件事,glreadpixel如何用来拍摄包含许多子视图(glkviews的uiviews)的glkview的快照??
    • @KaranSehgal 它不能。此外,没有 GLKViews 的 UIView 之类的东西。
    • 嘿,谢谢您的信息。非常感谢。我不知道opengl,我只是想要一个带有子视图的视图的截图。但是 renderInContext 方法需要很多时间,因为我经常截屏来制作视频。所以我在某处读到使用 opengl 可以解决我的问题。那么你对此有什么想法吗??
    • @KaranSehgal 如果您GLKView 包含子视图,您根本无法通过 OpenGL 捕获它们。在这种情况下,您必须使用另一种方法,例如 renderInContext:,顺便说一句,这是可用的最快方法。
    【解决方案2】:

    您可以使用 glreadPixels 读取背景屏幕。这是要执行的代码。

    - (UIImage*) getGLScreenshot {
        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;
    }
    
    - (void)saveGLScreenshotToPhotosAlbum {
        UIImageWriteToSavedPhotosAlbum([self getGLScreenshot], nil, nil, nil);
    }
    

    【讨论】:

      猜你喜欢
      • 2011-02-24
      • 2010-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-06
      相关资源
      最近更新 更多