iOS CoreGraphics:用半透明图案抚摸会导致颜色损坏

max*_*der 8 iphone core-graphics ios

我的任务是制作一个类似于擦除工具(用手指操作)的东西,它将显示背景图像而不是擦除图像.

这是我的源图像和目标图像(仅用于测试,实际图像将有所不同):

此搜索 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);   
}
Run Code Online (Sandbox Code Playgroud)

模式回调函数(**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);
}
Run Code Online (Sandbox Code Playgroud)

和绘图方法:

- (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);
}
Run Code Online (Sandbox Code Playgroud)

如果局部变量revealAlpha的值为1.0(如最后一个代码段所示),一切正常.但我需要擦除是半透明的(因此用户需要多次滑动同一个地方才能完全擦除它).这里出现了问题.如果我使revealAlpha小于1.0,则显示的目标图像看起来已损坏.

以下是revealAlpha == 1.0和revealAlpha == 0.1的结果:

图像2 http://img593.imageshack.us/img593/1809/69373874.png

如果你看得够近,你会发现右边图片上的蓝色渐变不再平滑.

Dun*_*n C 0

我同意另一张海报的观点。您的图像看起来并未损坏。相反,看起来您的前视图部分可见。作为测试,尝试将revealAlpha 更改为0.5。这样,用手指滑动一次会将前层的不透明度设置为 50%,第二次滑动会将其设置为 0%。会更容易知道发生了什么。