【问题标题】:In UIImage to Texture conversion, Texture is flipped vertically在 UIImage 到 Texture 的转换中,Texture 是垂直翻转的
【发布时间】:2020-03-20 14:18:46
【问题描述】:

我正在尝试将 UIImage 读入 iOS 平台上的纹理。我在 StackOverflow 上找到了可以解决问题的代码 sn-p,但问题是当我显示纹理时,它显示为镜像倒置。

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]; 

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

【问题讨论】:

  • 谢谢..它没有完全回答,但确实引导我找到了解决方案。
  • 如果你愿意,你可以接受你自己的解决方案。

标签: ios ios4 opengl-es-2.0


【解决方案1】:

这两行解决了问题

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多