Iphone iOS CoreGraphics:使用半透明图案抚摸会导致颜色损坏
我的任务是制作一个类似于擦除工具(用手指操作)的东西,它将显示背景图像,而不是被擦除的图像 以下是我的源图像和目标图像(仅供测试,真实图像会有所不同): 这是我的密码。创建模式:Iphone iOS CoreGraphics:使用半透明图案抚摸会导致颜色损坏,iphone,ios,core-graphics,Iphone,Ios,Core Graphics,我的任务是制作一个类似于擦除工具(用手指操作)的东西,它将显示背景图像,而不是被擦除的图像 以下是我的源图像和目标图像(仅供测试,真实图像会有所不同): 这是我的密码。创建模式: - (void)setFrame:(CGRect)frame { [super setFrame:frame]; if(revealPattern) CGPatternRelease(revealPattern); CGPatternCallbacks callbacks = { 0, &
- (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)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);
}
-(void)绘图点:(CGContextRef)上下文{
如果([点数]<2)返回;
CGPoint lastPoint=[[points objectAtIndex:0]CGPointValue];
CGContextBeginPath(上下文);
CGContextMoveToPoint(上下文,lastPoint.x,lastPoint.y);
CGContextSetBlendMode(上下文,kCGBlendModeNormal);
CGColorSpaceRef revealPatternSpace=CGColorSpaceCreatePattern(空);
CGContextSetStrokeColorSpace(上下文,revealPatternSpace);
cgla=1.0;
CGContextSetStrokePattern(上下文、revealPattern和revealAlpha);
CGContextSetAlpha(上下文,0.1);
CGColorSpaceRelease(显示模式空间);
CGContextSetLineCap(上下文,kCGLineCapRound);
CGContextSetLineJoin(上下文,kCGLineJoinRound);
CGContextSetLineWidth(上下文,self.lineWidth);
对于(int i=1;i<[点数];i++){
CGPoint currPoint=[[points objectAtIndex:i]CGPointValue];
CGContextAddLineToPoint(上下文,currPoint.x,currPoint.y);
lastPoint=currPoint;
}
CGContextStrokePath(上下文);
}
如果局部变量revealAlpha的值1.0(如最后一段代码所示),则一切正常。
但是我需要擦除是半透明的(因此用户需要在同一个地方刷几次才能完全擦除)。这里出现了问题。如果我将revealAlpha设置为小于1.0,则显示的目标图像看起来已损坏
以下是revealAlpha==1.0和revealAlpha==0.1的结果:
如果你看得足够近,你会注意到右图上的蓝色渐变不再平滑。我同意另一张海报。看起来您的图像没有损坏。相反,它看起来像是让前视图部分可见。作为测试,尝试将revealAlpha更改为.5。这样,用手指轻扫一次会将前一层的不透明度设置为50%,第二次轻扫会将其设置为0%。判断发生了什么会更容易。可能问题在于,在某些地方,revealAlpha永远无法达到1.0。(假设问题是多次滑动后,平滑的蓝色渐变从未显示。颜色损坏似乎是由于红色图像残留的痕迹…)这是否准确?
- (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);
}