问题描述
我正在使用 https://github.com/PaulSolt/UIImage-Conversion
但是,它似乎在alpha通道上有问题. 从RGBA像素字节数据重构UIImage的问题
However, it seems to have a problem on the alpha channel. Problem reconstituting UIImage from RGBA pixel byte data
我已经简化了这个问题,所以我将它作为一个更简洁的问题来呈现.
I have simplified the problem so I present it as a tidier question.
我下载了Paul的项目,运行它.它会在屏幕中央显示图像-到目前为止,一切都正确.
I download Paul's project, run it. It displays an image in the centre of the screen -- everything so far is correct.
但是他的演示并未测试Alpha.
But his demo is not testing for Alpha.
所以我尝试合成我的按钮图像...
So I attempt to composite my button image...
...在顶部.
结果是:
这是代码-我刚刚修改了AppDelegate_iPad.m:
here is the code -- I have just modified 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;
}
如果我切换标记为...的行
If I switch the line marked...
// <-- X
说...
... initWithImage:image]; // instead of imageCopy
...它应显示为:
有人在这方面有裂痕吗?它看起来非常好用,但是我不知道问题出在哪里.
Is anyone up having a crack at this? It looks a really nice library to use, but I can't figure out where the problem is.
推荐答案
我刚刚收到作者的回复:
I just got a reply from the author:
在他的博客文章的底部, http://paulsolt.com/2010/09/ios-converting-uiimage-to-rgba8-bitmaps-and-back/ Scott Davies提出了一个解决方法:
At the bottom of his blog post, http://paulsolt.com/2010/09/ios-converting-uiimage-to-rgba8-bitmaps-and-back/ Scott Davies presents a fix:
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
对此:
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedLast;
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedLast;
这篇关于在UIImage(PNG)和RGB8之间转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!