iOS CoreGraphics: 使用半透明图案进行描边会导致颜色失真。

8
我的任务是制作一个类似橡皮擦工具(用手指操作),它会显示一个背景图像,而不是被擦除的图像。
这是我的源图像和目标图像(只是为了测试,实际的图像可能不同): image1 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);   
}

模式回调函数(**info*包含指向*self*的指针):

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(如最后一段代码所示),则一切正常。但我需要擦除半透明(这样用户需要在同一位置多次滑动才能完全擦除它)。问题就在这里。如果我使revealAlpha小于1.0,则显示的目标图像看起来会出现错误。以下是revealAlpha == 1.0revealAlpha == 0.1的结果:

image2 http://img593.imageshack.us/img593/1809/69373874.png

如果你仔细观察,你会注意到右侧图片上的蓝色渐变不再平滑。


可能的问题是在某些地方revealAlpha从未达到1.0。(假设问题是在许多滑动之后,平滑的蓝色渐变从未被揭示。似乎颜色损坏是由于红色图像残留造成的...)这是否准确? - AMayes
1个回答

0

我同意其他帖子的观点。看起来你的图像并没有损坏,而是似乎你将前视图部分可见。作为测试,请尝试将revealAlpha更改为0.5。这样,用手指轻扫一次就会将前层的不透明度设置为50%,第二次轻扫将其设置为0%。这样可以更容易地了解发生了什么。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接