简体   繁体   English

CGImageCreateWithMask工作得很好,但在我生成的图像中,蒙面区域是黑色的,如何将其设置为白色?

[英]CGImageCreateWithMask works great but the masked out area is black in my resulting image, how can I set it to be white?

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];

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? 并且效果很好,但是生成的图像有黑色,它被掩盖了,我怎么能将它设置为白色掩盖了?

If you are masking JPEG image which does not have alpha channel this will happen (black background instead of transparent). 如果您正在屏蔽没有Alpha通道的JPEG图像,则会发生这种情况(黑色背景而不是透明)。

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 ... 一定要释放创建的图像,上下文和颜色空间......

The image is supposed to be transparent where it's "masked out"; 图像应该是透明的,它被“屏蔽掉”; the colour you see will depend on what background you're drawing it on. 你看到的颜色取决于你画的是什么背景。

(I don't remember if there's a requirement that the source image has an alpha channel.) (我不记得是否要求源图像具有alpha通道。)

It may be worth making sure that imageView.opaque = NO . 可能值得确保imageView.opaque = NO

Try this: 试试这个:

imageView.opaque = NO;
imageView.backgroundColor = [UIColor clearColor];

Also, the image used to mask should be black and white (not transparent). 此外,用于掩模的图像应为黑白(不透明)。

I was struggling with the same issue. 我正在努力解决同样的问题。 The solution is pretty simple: It is the best to create graphic context for masking this way: 解决方案非常简单:最好以这种方式创建用于屏蔽的图形上下文:

UIGraphicsBeginImageContextWithOptions(maskFrame.size, NO, 0.0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGRect ff =  CGRectMake(0, 0, maskFrame.size.width, mask.size.height);
CGContextClipToMask(ctx, ff, mask.CGImage);
....

Make sure, the second parameter in UIGraphicsBeginImageContextWithOptions is set to NO (which means none-opaque context). 确保, UIGraphicsBeginImageContextWithOptions中的第二个参数设置为NO (表示非透明上下文)。 Another benefit is that this creation function automatically scales context to retina displays. 另一个好处是这个创建功能自动缩放视网膜显示的上下文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM