本文介绍了CGImageCreateWithMask工作得很好,但在我生成的图像中,蒙面区域是黑色的,如何将其设置为白色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经掩盖了我的形象:
I've masked out my image thusly:
CGImageRef maskRef = [[UIImage imageNamed:@"testMask2.png"] CGImage];
CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
CGImageGetHeight(maskRef),
CGImageGetBitsPerComponent(maskRef),
CGImageGetBitsPerPixel(maskRef),
CGImageGetBytesPerRow(maskRef),
CGImageGetDataProvider(maskRef), nil, YES);
UIImage *image = [UIImage imageWithContentsOfFile:path];
CGImageRef masked = CGImageCreateWithMask([image CGImage], mask);
imageView.image = [UIImage imageWithCGImage:masked];
并且效果很好,但是生成的图像有BLACK,它被掩盖了,我该怎么设置它有掩盖的WHITE吗?
And it works great, but the resulting image has BLACK where it was masked out, how can I set it to have WHITE where its masked out?
推荐答案
如果你正在屏蔽没有alpha通道的JPEG图像,这将会发生(黑色背景而不是透明)。
If you are masking JPEG image which does not have alpha channel this will happen (black background instead of transparent).
所以你需要在屏蔽之前做这样的事情:
So you need to do something like this before masking:
CGImageRef imageNoAlpha = [UIImage imageNamed:@"noAlphaImage.jpg"].CGImage;
CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB();
CGFloat width = CGImageGetWidth(imageNoAlpha);
CGFloat height = CGImageGetHeight(imageNoAlpha);
CGContextRef ctxWithAlpha = CGBitmapContextCreate(nil, width, height, 8, 4*width, cs, kCGImageAlphaPremultipliedFirst);
CGContextDrawImage(ctxWithAlpha, CGRectMake(0, 0, width, height), imageNoAlpha);
CGImageRef imageWithAlpha = CGBitmapContextCreateImage(ctxWithAlpha);
CGImageRef masked = CGImageCreateWithMask(imageWithAlpha, mask);
...
一定要发布创建的图像,上下文和颜色空间......
Be sure to release created images, context and colorspace ...
这篇关于CGImageCreateWithMask工作得很好,但在我生成的图像中,蒙面区域是黑色的,如何将其设置为白色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!