iPhone पर एक पारदर्शी स्ट्रोक (या एक छवि के लिए वैसे भी स्पष्ट हिस्सा) आकर्षित करने के लिए कैसे

वोट
18

मुझे लगता है कि उपयोगकर्ता उंगली से स्क्रीन पर आकर्षित करने के लिए अनुमति देता है एक छोटा सा एप्लिकेशन है। मैं एक है UIImageViewजहाँ उपयोगकर्ता, ड्रॉ एक बनाने के द्वारा CGContextRefऔर विभिन्न CG drawकार्य करता है। मैं मुख्य रूप से समारोह के साथ स्ट्रोक / रेखाएंCGContextAddLineToPoint

अब मेरी समस्या यह है: उपयोगकर्ता विभिन्न रंगों की तर्ज आकर्षित कर सकते हैं। मैं उसे एक प्रयोग करने की क्षमता देना चाहता हूँ rubberउंगली से, उपकरण छवि अब तक तैयार के कुछ हिस्से को हटाने के लिए। मैं शुरू में (के साथ सेट स्ट्रोक के लिए एक सफेद रंग का उपयोग करके ऐसा किया CGContextSetRGBStrokeColorसमारोह), लेकिन यह ऐसा नहीं हो पाया ... क्योंकि मैं बाद में पता चला कि UIImageपर UIImageViewवास्तव में एक पारदर्शी पृष्ठभूमि, सफेद नहीं था ... तो मैं करूंगा उस पर सफेद लाइनों के साथ एक पारदर्शी छवि के साथ खत्म!

एक सेट करने के लिए वहाँ वैसे भी है transparentस्ट्रोक रंग या वहाँ की सामग्री स्पष्ट करने के लिए किसी अन्य तरीके से है CGContextRefउपयोगकर्ता की अंगुली के नीचे, जब वह यह बढ़ता रहता है? धन्यवाद

10/03/2009 को 10:18
का स्रोत उपयोगकर्ता
अन्य भाषाओं में...                            


6 जवाब

वोट
57

इस चाल करना होगा:

CGContextSetBlendMode(context, kCGBlendModeClear)
27/04/2010 को 19:29
का स्रोत उपयोगकर्ता

वोट
6

मैं Bresenham की लाइन एल्गोरिथ्म (प्राचीन काल के दिनों की है जब मैं अपने खुद के ग्राफिक्स दिनचर्या लिखना पड़ा करने के लिए वापस harkening) का उपयोग कर समाप्त हो गया ...

- (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) और drawingBoard (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 उचित मूल्यों के साथ की जगह) ...

04/04/2009 को 06:33
का स्रोत उपयोगकर्ता

वोट
1

मैं का उपयोग कर की कोशिश की है:

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

लेकिन वह काम नहीं करता है, क्योंकि यह करने के लिए एक अदृश्य रंग (और अदृश्य रंग + जो भी रंग उस पर आ रहा है = रंग उस पर ड्राइंग है) के साथ संदर्भ के शीर्ष पर "ड्राइंग" किया जा रहा है।

एकमात्र समाधान मैंने पाया है (जो इष्टतम नहीं है) है:

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

दुर्भाग्य से, इसका मतलब है कि आप rects की एक श्रृंखला का पता लगाने और अपने आप लाइन उत्पन्न करने के लिए है ...

30/03/2009 को 21:59
का स्रोत उपयोगकर्ता

वोट
0

आप स्पर्श के साथ छवि मिटाना चाहते हैं यह मेरा कोड है .... 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

}
28/03/2019 को 14:01
का स्रोत उपयोगकर्ता

वोट
-2
//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;
}
}
24/08/2011 को 16:25
का स्रोत उपयोगकर्ता

वोट
-13
UIColor *clearColor = [UIColor clearColor];
10/03/2009 को 15:07
का स्रोत उपयोगकर्ता

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more