0

我正在尝试使用图像(圆形 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() 有一些东西,但我不确定。

任何帮助,将不胜感激

谢谢你

4

1 回答 1

1

CGImageGetDataProvider 根本不改变数据。如果 pacmanImage 的数据与传递给 CGImageCreate 的参数 (bitsPer,bytesPer,colorSpace,...) 不完全匹配,则结果未定义。如果它完全匹配,则创建掩码将毫无意义。

您需要创建一个灰度 CGBitmapContext 来绘制蒙版,以及一个使用与位图相同的像素和参数的 CGImage。然后,您可以使用 CGImage 来掩盖另一个图像。

如果您想要继续修改的 CGBitmapContext 的快照,请仅使用 CGBitmapContextCreateImage。对于单次使用的位图,将相同的缓冲区传递给位图和您创建的匹配 CGImage。

编辑:

finalRect 是最终图像的大小。它要么足够大以容纳原始图像,并且 pacman 位于其中,要么足够大以容纳 pacman,并且原始图像被裁剪以适应。在此示例中,原始图像被裁剪。否则,pacman 路径必须相对于原始图像定位。

maskContext = CGBitmapContextCreate( ... , finalRect.size.width , finalRect.size.height , ... );
// add the pacman path and set the stroke and fill colors
CGContextDrawPath( maskContext , kCGPathFillStroke );
maskImage = CGBitmapContextCreateImage( maskContext );
imageToMask = CGImageCreateWithImageInRect( originalImage , finalRect );
finalImage = CGImageCreateWithMask( imageToMask , maskImage );
于 2010-05-31T03:12:10.400 回答