1

glReadPixels情報を入力してNSBitmapImageRepwitchを作成します。次のようになります。

    NSBitmapImageRep *imageRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL pixelsWide:width pixelsHigh:height bitsPerSample:8 samplesPerPixel:3 hasAlpha:NO isPlanar:NO colorSpaceName:NSDeviceRGBColorSpace bytesPerRow:3 * width bitsPerPixel:0];
    glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, [imageRep bitmapData]);

後でそれをNSDataに変換し、ファイルに書き込みます。しかし、私は逆さまの画像になります。どうすれば修正できますか?

4

2 に答える 2

1

ひっくり返ったに描画しNSImageます。これにより、逆さまの画像が正しい方向に描画されます。次に、のtiff表現を取得しNSImageます。これは、を使用して別の画像タイプに変更できNSBitmapImageRepますimageRepWithData:。新しい画像表現を使用して、新しいNSDataオブジェクトを使用して新しいオブジェクトを作成し、ファイルにrepresentationUsingType:properties:保存NSDataします。

    NSRect rect = self.bounds;
    NSSize size = rect.size;
    GLsizei width = (GLsizei)size.width;
    GLsizei height = (GLsizei)size.height;
    NSBitmapImageRep* imgRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL pixelsWide:width pixelsHigh:height bitsPerSample:8 samplesPerPixel:3 hasAlpha:NO isPlanar:NO colorSpaceName:NSDeviceRGBColorSpace bytesPerRow:width*3 bitsPerPixel:0];
    glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, [imgRep bitmapData]);
#ifdef __MAC_10_8
    NSImage* image = [NSImage imageWithSize:size flipped:YES drawingHandler:^(NSRect dstRect){
        return [imgRep drawInRect:dstRect];
    }];
#else
    NSImage* image = [[NSImage alloc] initWithSize:size];
    [image lockFocusFlipped:YES];
    [imgRep drawInRect:rect];
    [image unlockFocus];
#endif
    NSData* tiff = [image TIFFRepresentation];
于 2012-08-23T15:30:45.370 に答える
0

bytesPerRowは(width*3+3)&~3

于 2013-03-23T17:15:25.430 に答える