В преобразовании UIImage to Texture, текстура переворачивается вертикально - PullRequest
1 голос
/ 15 ноября 2011

Я пытаюсь прочитать 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]; 

Пожалуйста, дайте мне знать, что я здесь делаю неправильно.Спасибо.

1 Ответ

5 голосов
/ 16 ноября 2011

Эти две строки исправили проблему

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