UIImage 带有 mask/alpha 怪异
我在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论