创建和遮罩纯色 CGImage 时出现问题
在 iPhone
应用程序中,我有一组黑白图像。我需要一个函数将图像的白色区域更改为透明,将黑色区域更改为任意颜色。
我正在尝试创建纯色(黑色或白色)CGImageRef
,然后用我的原始图像掩盖它。它几乎可以工作了,但是应该透明的区域大约有 1% 透明。
这是我的功能。它接受一个布尔值来确定实体颜色应该是黑色还是白色。
- (UIImage*) imageWithMask:(UIImage *) maskImage andIsWhite:(BOOL) isWhite { CGImageRef maskImageRef = [maskImage CGImage]; CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGContextRef context = CGBitmapContextCreate(NULL, maskImage.size.width, maskImage.size.width, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast); if (isWhite) { CGContextSetRGBFillColor(context, (CGFloat)1.0, (CGFloat)1.0, (CGFloat)1.0, (CGFloat)1.0 ); } else { CGContextSetRGBFillColor(context, (CGFloat)0.0, (CGFloat)0.0, (CGFloat)0.0, (CGFloat)1.0 ); } CGContextAddRect(context, CGRectMake(0, 0, maskImage.size.width, maskImage.size.height)); CGContextFillRect(context, CGRectMake(0, 0, maskImage.size.width, maskImage.size.height)); CGImageRef solidImage = CGBitmapContextCreateImage(context); CGColorSpaceRelease(colorSpace); CGContextRelease(context); CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskImageRef), CGImageGetHeight(maskImageRef), CGImageGetBitsPerComponent(maskImageRef), CGImageGetBitsPerPixel(maskImageRef), CGImageGetBytesPerRow(maskImageRef), CGImageGetDataProvider(maskImageRef), NULL, false); CGImageRef masked = CGImageCreateWithMask(solidImage, mask); UIImage *maskedUIImage = [UIImage imageWithCGImage:masked]; CGImageRelease(masked); return maskedUIImage; }
任何建议表示赞赏。
In an iPhone
app I have a set of black and white images. I need a function which changes the white areas of the image to transparent and the black to an arbitrary color.
I'm trying to create a solid color (black or white) CGImageRef
and then masking it with my original image. It's almost working, but the areas which should be transparent are about 1% transparent.
Here is the function I have. It accepts a boolean
to determine whether the solid color
should be black or white.
- (UIImage*) imageWithMask:(UIImage *) maskImage andIsWhite:(BOOL) isWhite { CGImageRef maskImageRef = [maskImage CGImage]; CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGContextRef context = CGBitmapContextCreate(NULL, maskImage.size.width, maskImage.size.width, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast); if (isWhite) { CGContextSetRGBFillColor(context, (CGFloat)1.0, (CGFloat)1.0, (CGFloat)1.0, (CGFloat)1.0 ); } else { CGContextSetRGBFillColor(context, (CGFloat)0.0, (CGFloat)0.0, (CGFloat)0.0, (CGFloat)1.0 ); } CGContextAddRect(context, CGRectMake(0, 0, maskImage.size.width, maskImage.size.height)); CGContextFillRect(context, CGRectMake(0, 0, maskImage.size.width, maskImage.size.height)); CGImageRef solidImage = CGBitmapContextCreateImage(context); CGColorSpaceRelease(colorSpace); CGContextRelease(context); CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskImageRef), CGImageGetHeight(maskImageRef), CGImageGetBitsPerComponent(maskImageRef), CGImageGetBitsPerPixel(maskImageRef), CGImageGetBytesPerRow(maskImageRef), CGImageGetDataProvider(maskImageRef), NULL, false); CGImageRef masked = CGImageCreateWithMask(solidImage, mask); UIImage *maskedUIImage = [UIImage imageWithCGImage:masked]; CGImageRelease(masked); return maskedUIImage; }
Any suggestions are appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论