UIImage 带有 mask/alpha 怪异

发布于 2024-11-18 06:19:00 字数 2038 浏览 2 评论 0原文

我在使用 UIImage 时遇到了一些奇怪的情况。基本上,我是:

  • 将相册中的图像加载到 UIImage 中。
  • 创建一个由白色上的黑色椭圆组成的蒙版。
  • 使用 CGImageCreateWithMask 剪切掉原始 UIImage 的一部分。
  • 在屏幕上显示生成的 UIImage,它看起来是正确的。
  • 将相同的 UIImage 保存为 PNG。

但当我查看生成的 PNG 文件时,Alpha 却颠倒了!

有什么想法吗?

这是代码:

// originalImage is the UIImage loaded from the photo album

CGImageRef cgImage = [originalImage CGImage];

// make sure that this has an alpha channel    
CGImageRef shrunk1 = CopyImageAndAddAlphaChannel(shrunk);

// create oval mask
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGContextRef oc = CGBitmapContextCreate(NULL, desiredWidth, desiredHeight,
                                                8,
                                                desiredWidth * 4,
                                                colorspace,
                                                kCGImageAlphaNoneSkipLast);
// over white bgd
CGContextSetRGBFillColor(oc, 1.0, 1.0, 1.0, 1.0);
CGContextFillRect(oc, CGRectMake(0, 0, desiredWidth, desiredHeight));

// black oval
CGContextSetRGBFillColor(oc, 0.0, 0.0, 0.0, 1.0);
CGContextFillEllipseInRect(oc, CGRectMake(0, 0, desiredWidth, desiredHeight));
CGImageRef mask1 = CGBitmapContextCreateImage(oc);

CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(mask1), CGImageGetHeight(mask1),     CGImageGetBitsPerComponent(mask1), CGImageGetBitsPerPixel(mask1), CGImageGetBytesPerRow(mask1), CGImageGetDataProvider(mask1), NULL, NULL);

// cut out the original image with the oval mask
CGImageRef shrunk2 = CGImageCreateWithMask(shrunk1, mask);

// save it
UIImage *final = [UIImage imageWithCGImage:shrunk2];
NSString *s = [self getDateTimeFilename];
NSString *pngPath = [NSHomeDirectory() stringByAppendingFormat:@"/Documents/%@.png", s];
[UIImagePNGRepresentation(final) writeToFile:pngPath atomically:YES];

CGImageRelease(shrunk2);
CGImageRelease(mask);
CGImageRelease(mask1);
CGContextRelease(oc);
CGColorSpaceRelease(colorspace);

任何和所有帮助将不胜感激。谢谢!

I'm experiencing some weirdness with a UIImage. Basically, I'm:

  • Loading an image from the Photo album into a UIImage.
  • Creating a mask that consists of an black ellipse over white.
  • Using CGImageCreateWithMask to cut out a portion of the original UIImage.
  • Display the resulting UIImage onscreen and it looks correct.
  • Save out the same UIImage into a PNG.

But when I look at the resulting PNG file, the alpha is reversed!

Any ideas out there?

Here's the code:

// originalImage is the UIImage loaded from the photo album

CGImageRef cgImage = [originalImage CGImage];

// make sure that this has an alpha channel    
CGImageRef shrunk1 = CopyImageAndAddAlphaChannel(shrunk);

// create oval mask
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGContextRef oc = CGBitmapContextCreate(NULL, desiredWidth, desiredHeight,
                                                8,
                                                desiredWidth * 4,
                                                colorspace,
                                                kCGImageAlphaNoneSkipLast);
// over white bgd
CGContextSetRGBFillColor(oc, 1.0, 1.0, 1.0, 1.0);
CGContextFillRect(oc, CGRectMake(0, 0, desiredWidth, desiredHeight));

// black oval
CGContextSetRGBFillColor(oc, 0.0, 0.0, 0.0, 1.0);
CGContextFillEllipseInRect(oc, CGRectMake(0, 0, desiredWidth, desiredHeight));
CGImageRef mask1 = CGBitmapContextCreateImage(oc);

CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(mask1), CGImageGetHeight(mask1),     CGImageGetBitsPerComponent(mask1), CGImageGetBitsPerPixel(mask1), CGImageGetBytesPerRow(mask1), CGImageGetDataProvider(mask1), NULL, NULL);

// cut out the original image with the oval mask
CGImageRef shrunk2 = CGImageCreateWithMask(shrunk1, mask);

// save it
UIImage *final = [UIImage imageWithCGImage:shrunk2];
NSString *s = [self getDateTimeFilename];
NSString *pngPath = [NSHomeDirectory() stringByAppendingFormat:@"/Documents/%@.png", s];
[UIImagePNGRepresentation(final) writeToFile:pngPath atomically:YES];

CGImageRelease(shrunk2);
CGImageRelease(mask);
CGImageRelease(mask1);
CGContextRelease(oc);
CGColorSpaceRelease(colorspace);

Any and all help would be greatly appreciated. Thanks!

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文