iPhone에서 투명한 스트로크 (또는 이미지의 명확한 부분)를 그리는 방법

StackOverflow https://stackoverflow.com/questions/629409

  •  08-07-2019
  •  | 
  •  

문제

사용자가 손가락으로 화면을 그릴 수있는 작은 앱이 있습니다. 나는있다 UIImageView 사용자가 그리는 곳, a CGContextRef 그리고 다양한 CG draw 기능. 나는 주로 함수와 함께 스트로크/라인을 그립니다 CGContextAddLineToPoint

이제 내 문제는 이것입니다. 사용자는 다양한 색상의 라인을 그릴 수 있습니다. 나는 그에게 사용하는 능력을주고 싶다 "rubber" 손가락으로 지금까지 그린 이미지의 일부를 삭제하는 도구. 나는 처음에 스트로크에 흰색을 사용하여 이것을했다 ( CGContextSetRGBStrokeColor 기능)하지만 나중에 발견하지 않았기 때문에 UIImageUIImageView 실제로는 흰색이 아닌 투명한 배경을 가지고있었습니다 ... 그래서 나는 그것에 흰색 선이있는 투명한 이미지로 끝날 것입니다!

어쨌든 a를 설정할 수 있습니다 "transparent" 스트로크 색상 또는의 내용을 지우는 다른 방법이 있습니까? CGContextRef 사용자의 손가락 아래에서 움직일 때? 감사

도움이 되었습니까?

해결책

이것은 트릭을 수행 할 것입니다.

CGContextSetBlendMode(context, kCGBlendModeClear)

다른 팁

나는 Bresenham의 라인 알고리즘을 사용하게되었다 (내 그래픽 루틴을 작성해야 할 때 Yore의 날로 돌아 가기) ...

- (void) contextEraseLine:(CGContextRef) ctx from:(CGPoint)startPoint to:(CGPoint) endPoint withThickness:(int)thickness {
    int x, cx, deltax, xstep,
    y, cy, deltay, ystep,
    error, st, dupe;

    int x0, y0, x1, y1;

    x0 = startPoint.x;
    y0 = startPoint.y;
    x1 = endPoint.x;
    y1 = endPoint.y;

    // find largest delta for pixel steps
    st = (abs(y1 - y0) > abs(x1 - x0));

    // if deltay > deltax then swap x,y
    if (st) {
        (x0 ^= y0); (y0 ^= x0); (x0 ^= y0); // swap(x0, y0);
        (x1 ^= y1); (y1 ^= x1); (x1 ^= y1); // swap(x1, y1);
    }

    deltax = abs(x1 - x0);
    deltay = abs(y1 - y0);
    error  = (deltax / 2);
    y = y0;

    if (x0 > x1) { xstep = -1; }
    else         { xstep =  1; }

    if (y0 > y1) { ystep = -1; }
    else         { ystep =  1; }

    for ((x = x0); (x != (x1 + xstep)); (x += xstep))
    {
        (cx = x); (cy = y); // copy of x, copy of y

        // if x,y swapped above, swap them back now
        if (st) { (cx ^= cy); (cy ^= cx); (cx ^= cy); }

        (dupe = 0); // initialize no dupe

        if(!dupe) { // if not a dupe, write it out
            //NSLog(@"(%2d, %2d)", cx, cy);

            CGContextClearRect(ctx, CGRectMake(cx, cy, thickness, thickness));

    }

        (error -= deltay); // converge toward end of line

        if (error < 0) { // not done yet
            (y += ystep);
            (error += deltax);
        }
    }
}

휴! 그것은 (다소) 울퉁불퉁 한 지우개 라인을 만들기 위해 갈 길이 멀다.

사용하려면 다음과 같은 작업을 수행하십시오.

- (void)eraseStart {
    // erase lines
    UIGraphicsBeginImageContext(drawingBoard.size);
    ctx = UIGraphicsGetCurrentContext();
    CGContextDrawImage(ctx,CGRectMake(0,0,drawingBoard.size.width, drawingBoard.size.height),[drawingBoard CGImage]); 
}

- (void)eraseEnd {
    drawingBoard = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    [drawingView removeFromSuperview];
    [drawingView release];

    drawingView = [[UIImageView alloc] initWithImage:drawingBoard];
    drawingView.frame = CGRectMake(intEtchX, intEtchY, intEtchWidth, intEtchHeight);

    [self.view addSubview:drawingView];
}

이것은 이미 DrawingView (uiimageview)와 Drawgboard (uiimage)를 만들었다고 가정합니다.

그런 다음 선을 지우려면 다음과 같은 작업을 수행하십시오.

CGContextRef ctx = UIGraphicsGetCurrentContext();
[self eraseStart];
[self contextEraseLine:ctx from:CGPointMake (x1, y1) to:CGPointMake (x2, y2) withThickness:10];
[self eraseEnd];

(X1, Y1, X2 및 Y2를 적절한 값으로 바꾸십시오) ...

사용해 보았습니다 :

CGContextSetStrokeColorWithColor (myContext, [[UIColor clearColor] CGColor]);

그러나 그것은 보이지 않는 색상으로 컨텍스트 위에 "그리기"하는 것처럼 보이기 때문에 작동하지 않습니다.

내가 찾은 유일한 솔루션 (최적이 아닌)은 다음과 같습니다.

CGContextClearRect (myContext, CGRectMake(x, y, width, height));

불행히도, 그것은 당신이 일련의 낙타를 추적하고 직접 선을 생성해야한다는 것을 의미합니다 ...

터치로 이미지를 지우려면 이것이 내 코드입니다 .... Unerase 부품은 작동하지 않는 것이 좋지만 약간 느립니다.

func addNewPathToImage(){
    print("Start erasing or not erasing")

    UIGraphicsBeginImageContextWithOptions(TopImageUIImageView.bounds.size, false, UIScreen.main.scale) // or 0 for the scale

//My image is aspect fit so I need to get rect

    let aspect = TopImageUIImageView.image!.size.width / TopImageUIImageView.image!.size.height
    let viewRatio = TopImageUIImageView.bounds.size.width / TopImageUIImageView.bounds.size.height


    if aspect < viewRatio {
        let scale = TopImageUIImageView.bounds.size.height / TopImageUIImageView.image!.size.height
        let width = scale * TopImageUIImageView.image!.size.width
        let topLeftX = (TopImageUIImageView.bounds.size.width - width) * 0.5
        rect = CGRect(x: topLeftX, y: 0, width: width, height: TopImageUIImageView.bounds.size.height)
    }
    else {
        let scale = TopImageUIImageView.bounds.size.width / TopImageUIImageView.image!.size.width
        let height = scale * TopImageUIImageView.image!.size.height
        let topLeftY = (TopImageUIImageView.bounds.size.height - height) * 0.5
        rect = CGRect(x: 0.0, y: topLeftY, width: TopImageUIImageView.bounds.size.width, height: height)
    }
    ////
    context = UIGraphicsGetCurrentContext()
    TopImageUIImageView.image?.draw(in: rect)
    context?.setLineCap(.round)
    context?.setLineWidth(brushSize)



    if isErasing == true {

        context?.setShadow(offset: CGSize(width: 0, height: 0), blur: blurNumber)
        context?.setStrokeColor(UIColor.white.cgColor)
        context?.setBlendMode(.clear)


    }else{

        print("test the unerase image .... ?")
        context?.setStrokeColor(UIColor.init(patternImage: topImage.af_imageAspectScaled(toFit: CGSize(width: TopImageUIImageView.bounds.size.width, height: TopImageUIImageView.bounds.size.height))).cgColor)

    }


   // I am using these because I am using touch to define what to erase 
        context?.move(to: CGPoint(x: lastTouch.x, y: lastTouch.y))
        context?.addLine(to: CGPoint(x: currentTouch.x, y: currentTouch.y))
        context?.strokePath()
        context?.closePath() // when add this line or the "context?.beginPath" get ERROR CGContextClosePath: no current point.
        print("close the path")




        //get your image 
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        TopImageUIImageView.image = image

}
//erase part
if(mouseSwiped)
{

//**************Working Code*************// 


UIGraphicsBeginImageContext(frontImage.frame.size);
[frontImage.image drawInRect:CGRectMake(0, 0, frontImage.frame.size.width, frontImage.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(),kCGImageAlphaNone); //kCGImageAlphaPremultipliedLast);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 10);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1, 0, 0, 10);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextClearRect (UIGraphicsGetCurrentContext(), CGRectMake(lastPoint.x, lastPoint.y, 10, 10));
CGContextStrokePath(UIGraphicsGetCurrentContext());
frontImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();


lastPoint = currentPoint;

mouseMoved++;

if (mouseMoved == 10) 
{
    mouseMoved = 0;
}
}
UIColor *clearColor = [UIColor clearColor];
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top