3

opengl のコンテンツを (UIImage として) 取得し、ファイルに保存する方法を探しています。私は今、glReadPixels を試していますが、どの種類の malloc を行うべきかについて正しいことをしているかどうかはわかりません。OSXではGL_BGRAですが、iPhoneでは機能しないと収集しています...

4

3 に答える 3

2

すべての OpenGL|ES 準拠の GL 実装は、glReadPixels のパラメーターとして GL_RGBA をサポートする必要があります。

お使いの OpenGL|E が

GL_IMPLEMENTATION_COLOR_READ_FORMAT_OES 

拡張機能を使用すると、ネイティブ形式を照会することもできます。glReadPixels は、この形式を glReadPixels のパラメーターとして認識し、ネイティブ形式でピクセル データを直接取得できるようにします。これは通常、GL_RGBA よりも少し高速です。

ヘッダー ファイルを確認するか、glGetString を介して拡張機能のサポートを照会してください。

于 2008-11-24T14:46:58.610 に答える
1

「Technical Q&A QA1704 OpenGL ES View Snapshot」を参照してください。Apple は、Nils の「より良い方法」で画像を反転するなど、実用的な例を示しています。それはあなたをあなたのUIImageに導き、そこからNilsに従ってファイルとして保存します。

于 2011-04-15T15:01:53.520 に答える
1

OpenGL ES ビューからデータを取得します。

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

次に、画像を保存します。

-(void) saveImage:(UIImage *)image {}
    // Create paths to output images
    NSString  *pngPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.png"];
    NSString  *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.jpg"];

    // Write a UIImage to JPEG with minimum compression (best quality)
    // The value 'image' must be a UIImage object
    // The value '1.0' represents image compression quality as value from 0.0 to 1.0
    [UIImageJPEGRepresentation(image, 1.0) writeToFile:jpgPath atomically:YES];

    // Write image to PNG
    [UIImagePNGRepresentation(image) writeToFile:pngPath atomically:YES];
}
于 2010-07-26T09:39:00.967 に答える