我创建了一个用glReadPixels信息填充的NSBitmapImageRep。它看起来是这样的:
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并写入文件。但我的图像被颠倒了。我怎么才能修复它?
发布于 2012-08-23 15:30:45
绘制成翻转的NSImage
。这会将颠倒的图像绘制到正确的方向。然后,您将获得NSImage
的tiff表示,您可以使用另一个带有imageRepWithData:
的NSBitmapImageRep
将其更改为其他图像类型。使用新的图像表示法通过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];
发布于 2013-03-23 17:15:25
bytesPerRow应为(width*3+3)&~3
https://stackoverflow.com/questions/11948417
复制相似问题