我的任務是製作類似於擦除工具(用手指操作)的東西,它將顯示背景圖像而不是擦除圖像。iOS CoreGraphics:用半透明圖案撫摸會導致顏色腐敗
這裏是我的源和目標圖像(只是爲了測試,以假亂真會有所不同):
http://img232.imageshack.us/img232/6030/29572847.png
這裏是我的代碼。創建模式:
- (void)setFrame:(CGRect)frame {
[super setFrame:frame];
if(revealPattern) CGPatternRelease(revealPattern);
CGPatternCallbacks callbacks = { 0, &patternCallback, NULL};
revealPattern = CGPatternCreate(self, self.bounds, CGAffineTransformIdentity, self.bounds.size.width, self.bounds.size.height, kCGPatternTilingConstantSpacing, true, &callbacks);
}
模式回調函數(**信息*包含了指向自我):
void patternCallback(void *info, CGContextRef context) {
CGRect rect = ((DrawView *)info).bounds;
CGImageRef imageRef = ((DrawView *)info).backgroundImage.CGImage;
CGContextTranslateCTM(context, 0, rect.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextDrawImage(context, rect, imageRef);
}
和繪製方法:
- (void)drawPoints:(CGContextRef)context{
if([points count] < 2) return;
CGPoint lastPoint = [[points objectAtIndex: 0] CGPointValue];
CGContextBeginPath(context);
CGContextMoveToPoint(context, lastPoint.x, lastPoint.y);
CGContextSetBlendMode(context, kCGBlendModeNormal);
CGColorSpaceRef revealPatternSpace = CGColorSpaceCreatePattern(NULL);
CGContextSetStrokeColorSpace(context, revealPatternSpace);
CGFloat revealAlpha = 1.0;
CGContextSetStrokePattern(context, revealPattern, &revealAlpha);
CGContextSetAlpha(context, 0.1);
CGColorSpaceRelease(revealPatternSpace);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineJoin(context, kCGLineJoinRound);
CGContextSetLineWidth(context, self.lineWidth);
for(int i = 1; i < [points count]; i++){
CGPoint currPoint = [[points objectAtIndex: i] CGPointValue];
CGContextAddLineToPoint(context, currPoint.x, currPoint.y);
lastPoint = currPoint;
}
CGContextStrokePath(context);
}
如果本地變量revealAlpha具有值1.0(如la st代碼段),一切正常。 但我需要擦除是半透明的(以便用戶需要多次擦拭相同的位置才能完全擦除)。這裏出現問題。如果我製作revealAlpha小於1.0,則顯示的目標圖像看起來已損壞。
這裏是revealAlpha ==結果1.0和revealAlpha == 0.1:
http://img593.imageshack.us/img593/1809/69373874.png
如果你看夠近,你會發現右邊的藍色漸變畫面不再流暢。
據推測,問題是revealAlpha在某些地方永遠不會達到1.0。 (假設問題在於,在多次輕掃之後,平滑的藍色漸變從未透露出來,看起來顏色腐敗是由於剩下的紅色圖像的痕跡......)這是否準確? – AMayes 2012-04-19 17:59:17