2013-03-08 54 views
1

我正在使用[uiImage drawAtPoint]來繪製新圖像。但事實證明,顏色變了。我的原始圖像uiImage是紅色,而生成的圖像newImage是藍色。這裏是我的代碼:UIImage字節順序已更改

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

我猜測,生成的圖像採用RGB順序,而原來使用倒序。這是因爲白色和黑色的顏色保持不變。

編輯

我使用的OpenGL ES渲染這個紋理。

return [Texture createTexture: uiImage];而不是return [Texture createTexture: newImage]完美的作品,但我想畫uiImage上的文字。

我測試過UIImageWriteToSavedPhotosAlbum(newImage, nil, nil, nil);,保存的圖像是正確的。

我印的是CGImageGetBitmapInfo(uiImage.CGImage))CGImageGetBitmapInfo(newImage.CGImage),它們是不一樣的!

+0

我推薦使用UIGraphicsBeginImageContextWithOptions(uiImage.size,NO,0.0);而不是UIGraphicsBeginImageContext(uiImage.size);以支持視網膜屏幕渲染。 但是,這不應該是您的圖像渲染翻轉顏色的原因。我希望稍後再深入研究一下 - 儘快寫下來(只要尚未解決);)。 – Markus 2013-03-08 10:57:58

+0

@Markus它不能使用'UIGraphicsBeginImageContextWithOptions'。 – 2013-03-08 11:03:46

+0

你根本看不到任何圖像?請嘗試使用不透明YES而不是NO(第二參數),如果您不介意請分享結果。 – Markus 2013-03-08 13:22:40

回答

1

這是我的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; 
} 

電流和正確的版本,其中[ARRenderer 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); 
}