Question

I am implementing a image mask kind of feature in iOS similar to what is offered in the Blender app with two images. Here is my touch move code :-

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

    UITouch *touch = [touches anyObject];
    CGPoint currentPoint = [touch locationInView:staticBG1];

    UIGraphicsBeginImageContext(view.frame.size);
    [image_1 drawInRect:CGRectMake(0, 0, view.frame.size.width, view.frame.size.height)];

    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 20.0);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());

    image_1 = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    lastPoint = currentPoint;
    mouseMoved++;
    if (mouseMoved == 10)
        mouseMoved = 0;
}

The above code is generating an effect like :

Now what I really want is not the bright red line but pixels from another image in those places. Both images are of the same dimensions. How do I do it?? I tried to implement my manual image processing method my pixel access but it was too slow and this will be done in real time.

Is there any alternative to: CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0); ?

Was it helpful?

Solution

Don't draw a colour or a pattern into the path, draw transparency. You need one image in its own layer behind the image that is being wiped out. Create the path as you are now, but instead of setting the colour, set the blend mode to clear (kCGBlendModeClear).

This will remove sections of the image to allow you to see through to the image below.


Replace:

CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);

with:

CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear); 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top