如何擦掉一个蒸的视图

时间:2012-02-10 05:25:45

标签: ios uiview uiimage

我正在开发一款具有蒸汽视图的应用,用户可以用手指擦拭。我用它希望可以工作的东西对它进行了一次破解,它确实有效,但它的速度非常慢。下面的代码结合了绘图代码和屏蔽代码。绘图代码绘制为黑白图像,然后将其用作蒙版以遮挡雾图像。

是否有人知道任何示例代码可以达到此效果,或者对如何加快速度提出任何建议?

static CGPoint midPoint(CGPoint p1, CGPoint p2){
    return CGPointMake((p1.x+p2.x)*0.5f, (p1.y+p2.y)*0.5f);
}

- (UIImage *)maskImage:(UIImage *)image withMask:(UIImage *)maskImage{
    CGImageRef maskRef = [maskImage CGImage];
    CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
                                        CGImageGetHeight(maskRef),
                                        CGImageGetBitsPerComponent(maskRef),
                                        CGImageGetBitsPerPixel(maskRef),
                                        CGImageGetBytesPerRow(maskRef),
                                        CGImageGetDataProvider(maskRef), NULL, false);
    CGImageRef masked = CGImageCreateWithMask([image CGImage], mask);
    CGImageRelease(mask);
    UIImage *maskedImage = [UIImage imageWithCGImage:masked];
    CGImageRelease(masked);
    return maskedImage;

}

- (void)setMaskImage:(UIImage *)maskImage{
    [_maskImage release];
    _maskImage = [maskImage retain];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];

    _previousPoint1 = [touch previousLocationInView:self];
    _previousPoint2 = [touch previousLocationInView:self];
    _currentPoint = [touch locationInView:self];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];

    _previousPoint2 = _previousPoint1;
    _previousPoint1 = [touch previousLocationInView:self];
    _currentPoint = [touch locationInView:self];

    CGPoint mid1 = midPoint(_previousPoint1, _previousPoint2); 
    CGPoint mid2 = midPoint(_currentPoint, _previousPoint1);

    CGRect imageRect = CGRectZero;
    imageRect.size = _fogView.frame.size;
    UIGraphicsBeginImageContext(imageRect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [_maskImage drawInRect:imageRect];

    CGContextMoveToPoint(context, mid1.x, mid1.y);
    CGContextAddQuadCurveToPoint(context, _previousPoint1.x, _previousPoint1.y, mid2.x, mid2.y); 

    CGContextSetLineCap(context, kCGLineCapRound);
    CGContextSetLineWidth(context, 40.0f);
    CGContextSetRGBStrokeColor(context, 1.0f, 1.0f, 1.0f, 1.0f);
    CGContextStrokePath(context);

    [self setMaskImage:UIGraphicsGetImageFromCurrentImageContext()];
    UIGraphicsEndImageContext();

    _fogView.image = [self maskImage:[UIImage imageNamed:@"Fog"] withMask:_maskImage];
}

1 个答案:

答案 0 :(得分:0)

@badeen的帮助下,解决方案是使用两个CALayers,一个用于雾图像,一个用于蒙版,当用户在屏幕上移动手指时,将绘制蒙版。