Iphone UIImage字节顺序已更改

Iphone UIImage字节顺序已更改,iphone,ios,opengl-es,uiimage,cgimage,Iphone,Ios,Opengl Es,Uiimage,Cgimage,我正在使用[uiImage drawAtPoint]绘制新图像。但结果是颜色变了。我的原始图像uiImage为红色,而生成的图像newImage为蓝色。这是我的密码: UIImage* uiImage = [UIHelper loadImage:fileName]; NSString *text = [ApplicationSettings instance].user.name; UIGraphicsBeginImageContext(uiImage.size); [uiImage drawA

我正在使用
[uiImage drawAtPoint]
绘制新图像。但结果是颜色变了。我的原始图像
uiImage
为红色,而生成的图像
newImage
为蓝色。这是我的密码:

UIImage* uiImage = [UIHelper loadImage:fileName];
NSString *text = [ApplicationSettings instance].user.name;
UIGraphicsBeginImageContext(uiImage.size);
[uiImage drawAtPoint: CGPointZero];
[text drawAtPoint: CGPointMake(10, 10) withFont: [UIFont systemFontOfSize: 12]];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return [Texture createTexture: newImage];
[Texture createTexture]
中:

+ (Texture*) createTextureWithImage:(UIImage*)image {
    Texture* tex = [[[Texture alloc] init] autorelease];
    if ([tex loadImage:image])
        return tex;
    else {
        NSLog(@"Failed to load image %@", image);
        return nil;
    }
}

- (BOOL)loadImage:(UIImage *)image {
    BOOL ret = NO;

    if (image) {
        // Get the inner CGImage from the UIImage wrapper
        CGImageRef cgImage = image.CGImage;

        // Get the image size
        width = CGImageGetWidth(cgImage);
        height = CGImageGetHeight(cgImage);

        // Record the number of channels
        channels = CGImageGetBitsPerPixel(cgImage)/CGImageGetBitsPerComponent(cgImage);

        // Generate a CFData object from the CGImage object (a CFData object represents an area of memory)
        CFDataRef imageData = CGDataProviderCopyData(CGImageGetDataProvider(cgImage));

        // Copy the image data for use by Open GL
        ret = [self copyImageDataForOpenGL: imageData];
        CFRelease(imageData);
    }

    return ret;
}
[texture copyImageDataForOpenGL]

- (BOOL)copyImageDataForOpenGL:(CFDataRef)imageData {
    if (pngData) {
        free(pngData);
    }

    pngData = (unsigned char*)malloc(width * height * channels);
    const int rowSize = width * channels;
    const unsigned char* pixels = (unsigned char*)CFDataGetBytePtr(imageData);

    // Copy the row data from bottom to top
    for (int i = 0; i < height; ++i) {
        memcpy(pngData + rowSize * i, pixels + rowSize * (height - 1 - i), width * channels);
    }

    return YES;
}
-(BOOL)copyImageDataForOpenGL:(CFDataRef)imageData{
if(pngData){
自由(pngData);
}
pngData=(无符号字符*)malloc(宽度*高度*通道);
const int rowSize=宽度*通道;
const unsigned char*pixels=(unsigned char*)CFDataGetBytePtr(imageData);
//从下至上复制行数据
对于(int i=0;i
我猜生成的图像使用RGB顺序,而原始图像使用相反的顺序。这是因为白色和黑色保持不变

编辑

我正在使用OpenGL ES渲染此纹理

返回[Texture createTexture:uiImage]
而不是
返回[Texture createTexture:newImage]
效果很好,但我想在
uiImage
上绘制文本

我已经测试了
UIImageWriteToSavedPhotosAlbum(newImage,nil,nil,nil)并且保存的图像是正确的


我打印了
CGImageGetBitmapInfo(uiImage.CGImage))
CGImageGetBitmapInfo(newImage.CGImage)
,它们不一样

这是我的当前正确版本的
copyImageDataForOpenGL

- (BOOL)copyImageDataForOpenGL:(CGImageRef)image {
    if (pngData) {
        free(pngData);
    }

    pngData = (GLubyte*)malloc(width * height * channels);
    const int rowSize = width * channels;
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    NSUInteger bytesPerPixel = channels;
    NSUInteger bytesPerRow = bytesPerPixel * width;
    NSUInteger bitsPerComponent = 8;
    CGContextRef context = CGBitmapContextCreate(pngData, width, height,
                                                 bitsPerComponent, bytesPerRow, colorSpace,
                                                 kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
    CGColorSpaceRelease(colorSpace);
    CGContextClearRect(context, CGRectMake(0, 0, width, height));
    CGContextTranslateCTM(context, 0,0);
    CGContextDrawImage(context, CGRectMake(0, 0, width, height), image);

    [ARRenderer flipRGBData: pngData bytesPerRow:bytesPerRow height:height];
    return YES;
}
其中
[Arrender flipRGBData]
定义为

+ (void)flipRGBData:(GLubyte*)data bytesPerRow:(int)bytesPerRow height:(int)height {
    GLubyte* swp = (GLubyte*)malloc(bytesPerRow);
    for (int h = 0; h < height / 2; h++) {
        memcpy(swp, data + (height - 1 - h) * bytesPerRow, bytesPerRow);
        memcpy(data + (height - 1 - h) * bytesPerRow, data + h * bytesPerRow, bytesPerRow);
        memcpy(data + h * bytesPerRow, swp, bytesPerRow);
    }
    free(swp);
}
+(void)flipRGBData:(GLubyte*)数据bytesPerRow:(int)bytesPerRow height:(int)height{
GLubyte*swp=(GLubyte*)malloc(bytesPerRow);
对于(int h=0;h
这是我的当前正确版本的
copyImageDataForOpenGL

- (BOOL)copyImageDataForOpenGL:(CGImageRef)image {
    if (pngData) {
        free(pngData);
    }

    pngData = (GLubyte*)malloc(width * height * channels);
    const int rowSize = width * channels;
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    NSUInteger bytesPerPixel = channels;
    NSUInteger bytesPerRow = bytesPerPixel * width;
    NSUInteger bitsPerComponent = 8;
    CGContextRef context = CGBitmapContextCreate(pngData, width, height,
                                                 bitsPerComponent, bytesPerRow, colorSpace,
                                                 kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
    CGColorSpaceRelease(colorSpace);
    CGContextClearRect(context, CGRectMake(0, 0, width, height));
    CGContextTranslateCTM(context, 0,0);
    CGContextDrawImage(context, CGRectMake(0, 0, width, height), image);

    [ARRenderer flipRGBData: pngData bytesPerRow:bytesPerRow height:height];
    return YES;
}
其中
[Arrender flipRGBData]
定义为

+ (void)flipRGBData:(GLubyte*)data bytesPerRow:(int)bytesPerRow height:(int)height {
    GLubyte* swp = (GLubyte*)malloc(bytesPerRow);
    for (int h = 0; h < height / 2; h++) {
        memcpy(swp, data + (height - 1 - h) * bytesPerRow, bytesPerRow);
        memcpy(data + (height - 1 - h) * bytesPerRow, data + h * bytesPerRow, bytesPerRow);
        memcpy(data + h * bytesPerRow, swp, bytesPerRow);
    }
    free(swp);
}
+(void)flipRGBData:(GLubyte*)数据bytesPerRow:(int)bytesPerRow height:(int)height{
GLubyte*swp=(GLubyte*)malloc(bytesPerRow);
对于(int h=0;h
我建议使用UIGraphicsBeginImageContextWithOptions(uiImage.size,NO,0.0);而不是UIGraphicsBeginImageContext(uiImage.size);以支持视网膜屏幕渲染。但是,这不应成为图像渲染翻转颜色的原因。我想稍后再深入了解一下-尽快给您回信(只要尚未解决;)@Markus它无法使用
UIGraphicsBeginImageContextWithOptions
。您根本看不到任何图像吗?请尝试使用不透明的“是”而不是“否”(第二个参数),如果您不介意,请共享结果。@Markus我的意思是它保持不变。我建议使用UIGraphicsBeginImageContextWithOptions(uiImage.size,否,0.0);而不是UIGraphicsBeginImageContext(uiImage.size);以支持视网膜屏幕渲染。但是,这不应成为图像渲染翻转颜色的原因。我想稍后再深入了解一下-尽快给您回信(只要尚未解决;)@Markus它无法使用
UIGraphicsBeginImageContextWithOptions
。您根本看不到任何图像吗?请尝试使用不透明的“是”而不是“否”(第二个参数),如果您不介意,请共享结果。@Markus我的意思是它保持不变。