0

画像(270度の円、pacmanロゴに似ており、Core Graphicsとしてペイントされています)を使用してマスクを作成しようとしています。私がしているのはこれです

1.コアグラフィックスパスの作成

    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をこのメソッドの呼び出し元(CGImageRefであるwheelChoiceMadeStateとして)に返し、呼び出し元はCALayerのコンテンツプロパティを画像で更新します

 theLayer.contents                  = (id) wheelChoiceMadeState;

私が見ている問題は、マスクが適切に機能せず、実際に非常に奇妙に見えることです。CoreGraphicsによってペイントされたパス全体に奇妙なパターンが表示されます。私の勘では、CGImageGetDataProvider()に何かがあるのですが、よくわかりません。

どんな助けでもいただければ幸いです

ありがとうございました

4

1 に答える 1

1

CGImageGetDataProviderはデータをまったく変更しません。pacmanImageのデータがCGImageCreateに渡されたパラメーター(bitsPer、bytesPer、colorSpace、...)と正確に一致しない場合、結果は未定義です。完全に一致する場合は、マスクを作成しても意味がありません。

マスクを描画するためのグレースケールCGBitmapContextと、ビットマップと同じピクセルとパラメーターを使用するCGImageを作成する必要があります。次に、CGImageを使用して別の画像をマスクできます。

変更を続けるCGBitmapContextのスナップショットが必要な場合にのみ、CGBitmapContextCreateImageを使用してください。使い捨てビットマップの場合、作成したビットマップと一致するCGImageに同じバッファーを渡します。

編集:

finalRectは、最終的な画像のサイズです。元の画像を保持するのに十分な大きさで、パックマンがその中に配置されているか、またはパックマンを保持するのに十分な大きさで、元の画像が収まるようにトリミングされています。この例では、元の画像がトリミングされています。そうしないと、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 に答える