问题
I am using https://github.com/PaulSolt/UIImage-Conversion
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.
I download Paul's project, run it. It displays an image in the centre of the screen -- everything so far is correct.
But his demo is not testing for Alpha.
So I attempt to composite my button image...
... on top.
The result is:
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
to say...
... initWithImage:image]; // instead of imageCopy
... it displays as it should:
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.
回答1:
I just got a reply from the author:
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:
There’s a simple one-line fix for your code so that alpha channels are preserved when you go from the byte buffer to the UIImage. In convertBitmapRGBA8ToUIImage, change this:
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
to this:
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedLast;
来源:https://stackoverflow.com/questions/7177685/converting-between-uiimage-png-and-rgb8