RGBStroke - Image pixels instead of color (iOS)

狂风中的少年 提交于 2019-12-24 19:09:55

问题


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

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


回答1:


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


来源:https://stackoverflow.com/questions/19218422/rgbstroke-image-pixels-instead-of-color-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!