在UIImage(PNG)和RGB8之间转换

我正在使用https://github.com/PaulSolt/UIImage-Conversion

但是,它似乎在alpha通道上有问题。 从RGBA像素字节数据重构UIImage的问题

我已经简化了问题所以我把它作为一个更整洁的问题。

我下载了Paul的项目,运行它。 它在屏幕中央显示一个图像 – 到目前为止所有内容都是正确的。

但是他的演示没有测试Alpha。

所以我尝试合成我的按钮图像……

在此处输入图像描述

… 在上面。

结果是:

在此处输入图像描述

这是代码 – 我刚刚修改了AppDelegate_iPad.m:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. { UIImage *image = [UIImage imageNamed:@"Icon4.png"]; int width = image.size.width; int height = image.size.height; // Create a bitmap unsigned char *bitmap = [ImageHelper convertUIImageToBitmapRGBA8:image]; // Create a UIImage using the bitmap UIImage *imageCopy = [ImageHelper convertBitmapRGBA8ToUIImage:bitmap withWidth:width withHeight:height]; // Cleanup if(bitmap) { free(bitmap); bitmap = NULL; } // Display the image copy on the GUI UIImageView *imageView = [[UIImageView alloc] initWithImage:imageCopy]; CGPoint center = CGPointMake([UIScreen mainScreen].bounds.size.width / 2.0, [UIScreen mainScreen].bounds.size.height / 2.0); [imageView setCenter:center]; [window addSubview:imageView]; [imageView release]; } if( 1 ) { UIImage *image = [UIImage imageNamed:@"OuterButton_Dull.png"]; int width = image.size.width; int height = image.size.height; // Create a bitmap unsigned char *bitmap = [ImageHelper convertUIImageToBitmapRGBA8:image]; // Create a UIImage using the bitmap UIImage *imageCopy = [ImageHelper convertBitmapRGBA8ToUIImage:bitmap withWidth:width withHeight:height]; // Cleanup if(bitmap) { free(bitmap); bitmap = NULL; } // Display the image copy on the GUI UIImageView *imageView = [[UIImageView alloc] initWithImage:imageCopy]; // <-- X imageView.backgroundColor = [UIColor clearColor]; CGPoint center = CGPointMake([UIScreen mainScreen].bounds.size.width / 2.0, [UIScreen mainScreen].bounds.size.height / 2.0); [imageView setCenter:center]; [window addSubview:imageView]; [imageView release]; } [window makeKeyAndVisible]; return YES; } 

如果我切换标记的行…

 // <-- X 

说……

 ... initWithImage:image]; // instead of imageCopy 

……它显示应该:

在此处输入图像描述

有没有人对此有所了解? 它看起来是一个非常好用的库,但我无法弄清楚问题出在哪里。

我刚收到作者的回复:

在他的博客文章的底部, http : //paulsolt.com/2010/09/ios-converting-uiimage-to-rgba8-bitmaps-and-back/ Scott Davies提出了一个修复:

您的代码有一个简单的单行修复,以便在从字节缓冲区转到UIImage时保留Alpha通道。 在convertBitmapRGBA8ToUIImage中,更改此:

CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;

对此:

CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedLast;