如何将透明渐变蒙版添加到上下文中 [英] How to add a transparent gradient mask to a context

查看:147
本文介绍了如何将透明渐变蒙版添加到上下文中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用quartz 2d绘制饼图。

I am using quartz 2d to draw a pie chart.

我使用图层在底部绘制饼图的反射。

I use layer to draw a reflection of the pie chart on the bottom.

我想在反射中添加一个透明的alpha渐变,使其变得越来越透明,直到它变得不可见。

I would like to add a transparent alpha gradient to the reflection to make the it more and more transparent until it gets invisible.

某人有想法吗?

@EDIT:更多详情
我的饼图在CGLayerRef中。

@EDIT : more details My pie chart is in a CGLayerRef.

我首先将此图层绘制到CGContextRef。
然后我做了

I first draw this layer to the CGContextRef. Then I do a

CGContextTranslateCTM(context, 0, rect.size.height);
CGContextScaleCTM(context, 1.0, -1.0);

上下颠倒。

然后我把我的图层画了一个新的时间

Then i draw my layer a new time

提前致谢,

Loic

推荐答案

您需要使用图像蒙版。您可以通过在位图上下文中绘制渐变来制作蒙版:

You need to use an image mask. You can make the mask by drawing a gradient into a bitmap context:

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
CGContextRef gc = CGBitmapContextCreate(NULL, rect.size.width, rect.size.height, 8, rect.size.width, colorSpace, kCGImageAlphaNone);
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)[NSArray arrayWithObjects:(__bridge id)[UIColor whiteColor].CGColor, (__bridge id)[UIColor blackColor].CGColor, nil], NULL);
CGColorSpaceRelease(colorSpace);
CGContextDrawLinearGradient(gc, gradient, CGPointMake(0, 0), CGPointMake(0, rect.size.height), 0);
CGGradientRelease(gradient);
CGImageRef mask = CGBitmapContextCreateImage(gc);
CGContextRelease(gc);

(如果你不是,请删除 __ bridge 使用ARC。)

(Remove __bridge if you're not using ARC.)

然后你可以在绘制图像之前使用蒙版:

Then you can use the mask before drawing the image:

CGContextTranslateCTM(context, 0, rect.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextClipToMask(context, rect, mask);

完成后不要忘记释放面具。

Don't forget to release the mask after you're done.

这篇关于如何将透明渐变蒙版添加到上下文中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆