在iPhone App中使用CoreGraphic Stroke作为Alpha Mask

时间:2010-02-05 16:18:02

标签: iphone core-graphics mask

我基本上想要创建类似于非常简单的iSteam / iFog alebit版本的东西,用于不同的目的。实际上,将存在两个图像,一个主题,另一个是凝结图像或其他图像。然后,用户可以在屏幕上擦拭他们的手指,并且它将从顶层“切割”它以露出下层。到目前为止,我已经能够使用CoreGraphics和笔画在屏幕上绘制基本线,但我找不到一种方法可以将其用作蒸汽层的alpha蒙版。

如果有人能给我建议使用什么,或者甚至更好一些示例代码,我会非常感激,因为现在我正在拔头发。以下是我到目前为止的情况:


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

mouseSwiped = YES;
UITouch *touch = [touches anyObject];   
CGPoint currentPoint = [touch locationInView:self.view];
UIGraphicsBeginImageContext(self.view.frame.size);
CGRect rect = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[drawImage.image drawInRect:rect];
CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, 36.0);
CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
CGContextBeginPath(context);

CGContextMoveToPoint(context, lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(context, currentPoint.x, currentPoint.y);
CGContextStrokePath(context);
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();

}

1 个答案:

答案 0 :(得分:3)

您要调用的方法是CGContextClipToMask。只需绘制您的“蒸汽”图像,然后在另一个CGImage上绘制笔划。然后,将蒸汽夹到冲程中。像这样:

- (void)somewhereElse{
    UIImage *steam = [[UIImage imageNamed:@"steamImage.png"] retain];
    steamRef = steam.CGImage;
    //...
}

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextDrawImage(context, self.bounds, steamRef);         //draw the main image
    CGContextClipToMask(context, self.bounds, maskRef);         //respect alpha mask
    //where maskRef is a GCImage of your stroked path
}