在iPhone App中使用CoreGraphic Stroke作为Alpha Mask [英] Use a CoreGraphic Stroke as Alpha Mask in iPhone App

查看:119
本文介绍了在iPhone App中使用CoreGraphic Stroke作为Alpha Mask的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm basically looking to create something akin to a very simple version of iSteam/iFog alebit for a different purpose. In effect there will be two images, one of the subject matter and the other an image of condensation or some such. The user can then wipe their finger over the screen and it will "cut" that from the top layer to reveal the lower layer. So far I've been able to draw a basic line on the screen using CoreGraphics and strokes but I can't find a way to then use this as an alpha mask for the steam layer.

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

If anyone could give me advice on what to use or even better some sample code I'd be very grateful as right now I'm pulling my hair out. Here is what I have so far:


- (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();

}

推荐答案

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

The method you want to call is CGContextClipToMask. Just draw your "steam" image and then the stroke on another CGImage. Then, clip the steam to the stroke. Something like this:

- (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
}

这篇关于在iPhone App中使用CoreGraphic Stroke作为Alpha Mask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆