我的任务是制作一个类似于擦除工具(用手指操作)的东西,它会显示背景图像而不是删除图像。
以下是我的源图像和目标图像(仅供测试,实际图像会有所不同):
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.0 和 revealAlpha == 0.1 的结果:
http://img593.imageshack.us/img593/1809/69373874.png
如果你看得够近,你会注意到右边图片上的蓝色渐变不再平滑。
答案 0 :(得分:0)
我同意另一张海报。它看起来不像您的图像已损坏。相反,它看起来像是让你的前视图部分可见。作为测试,尝试将revealAlpha更改为.5。这样,用手指轻扫一下就会将前层的不透明度设置为50%,第二次滑动会将其设置为0%。它会更容易分辨出发生了什么。