UIImage (PNG) 和 RGB8 之间的转换

发布于 2024-12-01 08:17:52 字数 3164 浏览 4 评论 0原文

我正在使用 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

...它按应有的方式显示:

在此处输入图像描述

有人在吗有裂缝吗?它看起来非常好用,但我不知道问题出在哪里。

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...

enter image description here

... on top.

The result is:

enter image description here

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:

enter image description here

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少女净妖师 2024-12-08 08:17:52

我刚刚收到作者的回复:

在他的博客文章的底部,

对您的代码有一个简单的一行修复,以便 alpha 通道
当您从字节缓冲区转到 UIImage 时保留。在
将BitmapRGBA8ToUIImage转换为:

CGBitmapInfo 位图信息 = kCGBitmapByteOrderDefault;

对此:

CGBitmapInfo 位图信息 = kCGBitmapByteOrderDefault |
kCGImageAlphaPremultipliedLast;

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;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文