2010-05-31 102 views
0

我正在嘗試使用圖像(270度的圓,類似於pacman徽標,繪製爲核心圖形)創建蒙版。我做的是這樣的CGImageCreateWithMask以圖像作爲蒙版

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該方法的調用者(如wheelChoiceMadeState,這是一個CGImageRef)誰然後向上紅棗的CALayer的內容屬性與圖像

theLayer.contents     = (id) wheelChoiceMadeState; 

我看到的問題是,面具不能正常工作,看起來非常奇怪了。我在Core Graphics繪製的路徑上出現了奇怪的圖案。我的預感是有一些與CGImageGetDataProvider(),但我不知道。

任何幫助,將不勝感激

謝謝

回答

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); 
+0

感謝Drawnonward。我是iphone和Core Graphics的新手,所以你的回覆雖然不完全清楚,但仍然很清晰。我設法玩弄。我使用bitsPerComponent \t = 8; bitsPerPixel = bitsPerComponent * 1創建了一個CGBitmapContextCreate; bytesPerRow =(CGImageGetWidth(pacmanImage)* 8)和CGColorSpaceCreateDeviceGray()。然後我繪製了一個路徑進入該位圖。那麼pacmanImage \t = CGBitmapContextCreateImage(mybitmapContext);然後CGContextDrawImage(context,imgRectBox,pacmanImage); CGImageRef maskedImageFinal \t = CGBitmapContextCreateImage(context); 但pacmanImage的座標是錯誤的 – Peyman 2010-06-01 03:21:34

+0

您需要創建兩個尺寸相同的圖像傳遞到CGImageCreateWithMask。您可以創建大小爲imgToMask的CGBitmapContext,或者使用CGImageCreateWithImageInRect將imgToMask裁剪爲較小的大小,並將該大小用於位圖上下文。一旦您將pacman繪製到位圖上下文中並使用CGBitmapContextCreateImage從其創建圖像,請將該圖像作爲掩碼傳遞給CGImageCreateWithMask。 – drawnonward 2010-06-01 04:29:04

+0

我正在這樣做 1- mybitmapContext = MyCreateBitmapContext(imgToMask.size.width,imgToMask.size.height,theLayer); 2-繪製pacman 3- pacmanImage \t = CGBitmapContextCreateImage(mybitmapContext); 4- mask \t = CGImageCreate(.. mask mask params) 5- imageMaskedWithImage \t = CGImageCreateWithMask(imgToMaskRef,mask); 6- CGContextDrawImage(context,imgRectBox,imageMaskedWithImage); 012-CGImageRef maskedImageFinal = CGBitmapContextCreateImage(context); 8-返回maskedImageFinal; then 9-theLayer.contents =(id)maskedImageFinal; 我不需要使用CGImageCreate()來創建一個蒙版? 謝謝 – Peyman 2010-06-01 04:56:02