我正在尝试使用图像(圆形 270 度,类似于 pacman 徽标,绘制为核心图形)来创建蒙版。我正在做的是这个
1.创建Core Graphics路径
CGContextSaveGState(context);
CGContextBeginPath(context);
CGContextMoveToPoint(context,circleCenter.x,circleCenter.y);
//CGContextSetAllowsAntialiasing(myBitmapContext, YES);
CGContextAddArc(context,circleCenter.x, circleCenter.y,circleRadius,startingAngle, endingAngle, 0); // 0 is counterclockwise
CGContextClosePath(context);
CGContextSetRGBStrokeColor(context,1.0,0.0,0.0,1.0);
CGContextSetRGBFillColor(context,1.0,0.0,0.0,0.2);
CGContextDrawPath(context, kCGPathFillStroke);
2. 然后我正在创建一个路径的图像,其中包含刚刚绘制的路径
CGImageRef pacmanImage = CGBitmapContextCreateImage (context);
3.恢复上下文
CGContextRestoreGState(context);
CGContextSaveGState(context);
4. 创建一个 1 位掩码(将提供黑白掩码)
bitsPerComponent = 1;
bitsPerPixel = bitsPerComponent * 1 ;
bytesPerRow = (CGImageGetWidth(imgToMaskRef) * bitsPerPixel);
mask = CGImageCreate(CGImageGetWidth(imgToMaskRef),
CGImageGetHeight(imgToMaskRef),
bitsPerComponent,
bitsPerPixel,
bytesPerRow,
greyColorSpace,
kCGImageAlphaNone,
CGImageGetDataProvider(pacmanImage),
NULL, //decode
YES, //shouldInterpolate
kCGRenderingIntentDefault);
5. 用刚刚创建的掩码屏蔽imgToMaskRef(这是一个CGImageRef imgToMaskRef =imgToMask.CGImage;)
imageMaskedWithImage = CGImageCreateWithMask(imgToMaskRef, mask);
CGContextDrawImage(context,imgRectBox, imageMaskedWithImage);
CGImageRef maskedImageFinal = CGBitmapContextCreateImage (context);
6. 将 maskedImageFinal 返回给该方法的调用者(作为 wheelChoiceMadeState,它是一个 CGImageRef),然后使用图像更新 CALayer 内容属性
theLayer.contents = (id) wheelChoiceMadeState;
我看到的问题是面具不能正常工作,看起来确实很奇怪。我在 Core Graphics 绘制的路径上看到奇怪的图案。我的预感是 CGImageGetDataProvider() 有一些东西,但我不确定。
任何帮助,将不胜感激
谢谢你