ios - 在UIImage到Texture的转换中,Texture垂直翻转

标签 ios ios4 opengl-es-2.0

我正在尝试将UIImage读入iOS平台的纹理中。我在StackOverflow上找到了能解决问题的代码片段,但是问题是当我显示纹理时,它以镜像倒置的方式显示。

int numComponents = 4;

UIImage* image = [UIImage imageNamed:@"Test.png"];
CGImageRef imageRef = [image CGImage];
int width = CGImageGetWidth(imageRef);
int height = CGImageGetHeight(imageRef);

//Allocate texture data
GLubyte* textureData = (GLubyte *)malloc(width * height * numComponents);    

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(textureData, width, height,
                                             bitsPerComponent, bytesPerRow, colorSpace,
                                             kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);

CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(context);

//texture setup
GLuint textureID;    
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(1, &textureID);

glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureDataMirrored);   

我也尝试使用下面的行(在读取数据之前)镜像UIImage,但是它也不起作用。实际上没有任何作用。
image = [UIImage imageWithCGImage:image.CGImage scale:image.scale orientation:UIImageOrientationUpMirrored]; 

请让我知道我在做什么错。谢谢。

最佳答案

这两行解决了问题

CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0,height);
CGContextConcatCTM(context, flipVertical);

关于ios - 在UIImage到Texture的转换中,Texture垂直翻转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8129718/

相关文章:

ios - iOS 上的线程安全惰性初始化

ios - UITableView 和 UITableViewDataSource

iphone - plist段控制保存问题

ios - 如何在运行应用程序之前加载几秒钟的图片?

ios - iOS地址簿的奇怪内存泄漏

ios4 - 如何在iPhone应用程序的UIWebView中启用 "select all"?

iphone - 以编程方式隐藏 UITabBar

c++ - 是否需要创建OpenGL情侣程序?

ios - 如何从iPhone/iPad的一个屏幕显示多个EAGLView?

android - 回收从 'BitmapDrawable' 获取的位图时出现问题