2012-08-14 38 views
1

我創建了NSBitmapImageRep witch我填寫了glReadPixels信息。它看起來像這樣:glReadPixels - > NSData - >保存圖像

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並寫入文件。但是我倒過來倒像。我該如何解決它?

回答

1

畫成翻轉的NSImage。這會將正面朝上的圖像繪製到正確的方向。然後,您將獲得NSImage的tiff表示,您可以使用另一個NSBitmapImageRepimageRepWithData:更改爲其他圖像類型。使用新的圖像表示法使用representationUsingType:properties:製作新的NSData對象,並將新的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]; 
0

bytesPerRow應該(width*3+3)&~3

相關問題