【问题标题】:erase drawing that is saved to another image view Xcode擦除保存到另一个图像视图 Xcode 的绘图
【发布时间】:2014-11-29 17:13:38
【问题描述】:

我正在使用以下代码通过 UITouch 在图像上绘图。这工作正常,我将图像从临时 imageView 保存到另一个图像视图。问题是我想返回并从保存的 imageView 中删除一些绘图。我知道我可以将颜色设置为白色,它似乎会擦除它,但我正在另一个包含我的相机胶卷图像的 imageView 之上绘图。所以我需要完全清除保存的图纸。

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



    self.RealImage.frame = CGRectMake(14-(ax2*subtract),138-(ay2*subtract),740*see,850*see);

    self.imageView.frame = CGRectMake(14-(ax2*subtract),138-(ay2*subtract),740*see,850*see);

    self.tempImage.frame = CGRectMake(14-(ax2*subtract),138-(ay2*subtract),740*see,850*see);



    ctr = 0;
    UITouch *touch = [touches anyObject];
    pts[0] = [touch locationInView:self.tempImage];

    lastPoint = [touch locationInView:tempImage];
}


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


    self.RealImage.frame = CGRectMake(14-(self.zoom),138-(self.first),740*see,850*see);

    self.imageView.frame = CGRectMake(14-(self.zoom),138-(self.first),740*see,850*see);

    self.tempImage.frame = CGRectMake(14-(self.zoom),138-(self.first),740*see,850*see);


    UITouch *touch = [touches anyObject];
    CGPoint p = [touch locationInView:self.tempImage];
    currentPoint = [touch locationInView:tempImage];
    ctr++;
    pts[ctr] = p;

    if (ctr == 4)
    {
        pts[3] = CGPointMake((pts[2].x + pts[4].x)/2.0, (pts[2].y + pts[4].y)/2.0); // move the endpoint to the middle of the line joining the second control point of the first Bezier segment and the first control point of the second Bezier segment
        [path moveToPoint:pts[0]];
        [path addCurveToPoint:pts[3] controlPoint1:pts[1] controlPoint2:pts[2]]; // add a cubic Bezier from pt[0] to pt[3], with control points pt[1] and pt[2]

        [self draw2];


        // replace points and get ready to handle the next segment
        pts[0] = pts[3];
        pts[1] = pts[4];
        ctr = 1;
    }
     NSLog(@"ctr:%d",ctr);
    lastPoint = currentPoint;
}



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

    king  = pencilString;

    [path removeAllPoints];
    ctr = 0;



    UIGraphicsBeginImageContext(self.tempImage.frame.size);
    self.RealImage.frame = CGRectMake(14-(self.zoom),138-(self.first),740*see,850*see);

    self.imageView.frame = CGRectMake(14-(self.zoom),138-(self.first),740*see,850*see);

    self.tempImage.frame = CGRectMake(14-(self.zoom),138-(self.first),740*see,850*see);
    [self.imageView.image drawInRect:CGRectMake(0,0, self.imageView.frame.size.width, self.imageView.frame.size.height) blendMode:kCGBlendModeNormal alpha:1.0];
    [self.tempImage.image drawInRect:CGRectMake(0,0, self.tempImage.frame.size.width, self.tempImage.frame.size.height) blendMode:kCGBlendModeNormal alpha:1.0];

    self.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
    self.tempImage.image = nil;
    if (see ==2) {


    self.RealImage.frame = CGRectMake(14-(self.zoom),138-(self.first),1480,1700);

    self.imageView.frame = CGRectMake(14-(self.zoom),138-(self.first),1480,1700);

        self.tempImage.frame = CGRectMake(14-(self.zoom),138-(self.first),1480,1700);}


    UIGraphicsEndImageContext();



}

- (void)draw2
{



    self.RealImage.frame = CGRectMake(14-(self.zoom),138-(self.first),740*see,850*see);

    self.imageView.frame = CGRectMake(14-(self.zoom),138-(self.first),740*see,850*see);

    self.tempImage.frame = CGRectMake(14-(self.zoom),138-(self.first),740*see,850*see);
    UIGraphicsBeginImageContext(self.tempImage.frame.size);
    [self.tempImage.image drawInRect:CGRectMake(0, 0, self.tempImage.frame.size.width, self.tempImage.frame.size.height)];

    pts[3] = CGPointMake((pts[2].x + pts[4].x)/2.0, (pts[2].y + pts[4].y)/2.0); // move the endpoint to the middle of the line joining the second control point of the first Bezier segment and the first control point of the second Bezier segment
    [path moveToPoint:pts[0]];
    [path addCurveToPoint:pts[3] controlPoint1:pts[1] controlPoint2:pts[2]]; // add a cubic Bezier from pt[0] to pt[3], with control points pt[1] and pt[2]

//这是我尝试创建擦除的地方,但我得到的只是一条黑线

    if ([pencilString isEqualToString:@"clear2"]) {

        mode = DrawingModePen;
        if (mode == DrawingModePen) {
            NSLog(@"drawing");
            CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [[UIColor clearColor
                                                                              ] CGColor]);
        }

        UIGraphicsBeginImageContext(self.imageView.frame.size);



        [self.imageView.image drawInRect:CGRectMake(0, 0, self.imageView.frame.size.width, self.imageView.frame.size.height)];
       CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);

    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);

        self.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
        [self.imageView setAlpha:1.0];

        [path stroke];

        CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeCopy);
           }
    else if ([pencilString isEqualToString:@"red"]) {
        [[UIColor colorWithRed:255.0/255.0 green:0.0 blue:0.0 alpha:1.0] setStroke];
    }

    else if ([pencilString isEqualToString:@"blue"]) {
        [[UIColor colorWithRed:51.0/255.0 green:76.0/255.0 blue:255.0/255.0 alpha:1.0] setStroke];
    }

    else if ([pencilString isEqualToString:@"yellow"]) {
        [[UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:0.0 alpha:1.0] setStroke];
    }

    else if ([pencilString isEqualToString:@"orange"]) {
        [[UIColor colorWithRed:255.0/255.0 green:90.0/255.0 blue:0.0 alpha:1.0] setStroke];
    }

    else if ([pencilString isEqualToString:@"green"]) {
        [[UIColor colorWithRed:0.0 green:204.0/255.0 blue:0.0 alpha:1.0] setStroke];
    }

    else if ([pencilString isEqualToString:@"purple"]) {
        [[UIColor colorWithRed:153.0/255.0 green:0.0 blue:255.0/255.0 alpha:1.0] setStroke];
    }

    else if ([pencilString isEqualToString:@"redOrange"]) {
        [[UIColor colorWithRed:255.0/255.0 green:51.0/255.0 blue:0.0 alpha:1.0] setStroke];
    }

    else if ([pencilString isEqualToString:@"yellowOrange"]) {
        [[UIColor colorWithRed:255.0/255.0 green:153.0/255.0 blue:0.0 alpha:1.0] setStroke];
    }

    else if ([pencilString isEqualToString:@"blueViolet"]) {
        [[UIColor colorWithRed:102.0/255.0 green:51.0/255.0 blue:255.0/255.0 alpha:1.0] setStroke];
    }

    else if ([pencilString isEqualToString:@"blueGreen"]) {
        [[UIColor colorWithRed:0.0 green:153.0/255.0 blue:204.0/255.0 alpha:1.0] setStroke];
    }

    else if ([pencilString isEqualToString:@"redViolet"]) {
        [[UIColor colorWithRed:255.0/255.0 green:0.0 blue:153.0/255.0 alpha:1.0] setStroke];
    }

    else if ([pencilString isEqualToString:@"yellowGreen"]) {
        [[UIColor colorWithRed:204.0/255.0 green:255.0/255.0 blue:0.0 alpha:1.0] setStroke];
    }

    else if ([pencilString isEqualToString:@"white"]) {
        [[UIColor whiteColor] setStroke];
    }

    else if ([pencilString isEqualToString:@"ltGray"]) {
        [[UIColor colorWithRed:204.0/255.0 green:204.0/255.0 blue:204.0/255.0 alpha:1.0] setStroke];
    }

    else if ([pencilString isEqualToString:@"dkGray"]) {
        [[UIColor colorWithRed:102.0/255.0 green:102.0/255.0 blue:102.0/255.0 alpha:1.0] setStroke];
    }

    else if ([pencilString isEqualToString:@"brown"]) {
        [[UIColor colorWithRed:102.0/255.0 green:51.0/255.0 blue:51.0/255.0 alpha:1.0] setStroke];
    }

    else if ([pencilString isEqualToString:@"tan"]) {
        [[UIColor colorWithRed:255.0/255.0 green:204.0/255.0 blue:102.0/255.0 alpha:1.0] setStroke];
    }

    else if ([pencilString isEqualToString:@"black"]) {
        [[UIColor blackColor] setStroke];
    }

    [path stroke];

    CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);

    //    CGContextStrokePath(UIGraphicsGetCurrentContext());
    self.tempImage.image = UIGraphicsGetImageFromCurrentImageContext();
    [self.tempImage setAlpha:1.0];
    self.RealImage.frame = CGRectMake(14-(self.zoom),138-(self.first),740*see,850*see);

    self.imageView.frame = CGRectMake(14-(self.zoom),138-(self.first),740*see,850*see);

    self.tempImage.frame = CGRectMake(14-(self.zoom),138-(self.first),740*see,850*see);

    UIGraphicsEndImageContext();



}

【问题讨论】:

    标签: ios6 xcode6 cgcontext uibezierpath


    【解决方案1】:

    我想通了。我将“clear2”擦除的 if 语句放在 -(void) draw2{ 的开头,通过在 imageView 上绘制并将 blendMode 设置为 kCGBlendModeCopy 来重写它。然后我将其他所有内容都放在 else 语句中,它就可以工作了。
    这是代码。

    - (void)draw2
    {
    
    if ([pencilString isEqualToString:@"clear2"]) {
    
        UIGraphicsBeginImageContext(imageView.frame.size);
        [[UIColor clearColor] setStroke];
        [imageView.image drawInRect:CGRectMake(0,0,imageView.frame.size.width, imageView.frame.size.height)];
    
    
    
    
        CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeCopy);
    
    
    
        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    
        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    
        CGContextStrokePath(UIGraphicsGetCurrentContext());
    
         [path setLineWidth:20.0];
    
    
            [path stroke];
    
    
    imageView.image = UIGraphicsGetImageFromCurrentImageContext();
    
    
    
    }
    else {
    
    UIGraphicsBeginImageContext(self.tempImage.frame.size);
    [self.tempImage.image drawInRect:CGRectMake(0, 0, self.tempImage.frame.size.width, self.tempImage.frame.size.height)];
    
    pts[3] = CGPointMake((pts[2].x + pts[4].x)/2.0, (pts[2].y + pts[4].y)/2.0); // move the endpoint to the middle of the line joining the second control point of the first Bezier segment and the first control point of the second Bezier segment
    [path moveToPoint:pts[0]];
    [path addCurveToPoint:pts[3] controlPoint1:pts[1] controlPoint2:pts[2]]; // add a cubic Bezier from pt[0] to pt[3], with control points pt[1] and pt[2]
    
    
    
    
    if ([pencilString isEqualToString:@"red"]) {
        [[UIColor colorWithRed:255.0/255.0 green:0.0 blue:0.0 alpha:1.0] setStroke];
        [path setLineWidth:10.0*see];
    }
    [path stroke];
    
    CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);
    
    //    CGContextStrokePath(UIGraphicsGetCurrentContext());
    self.tempImage.image = UIGraphicsGetImageFromCurrentImageContext();
    [self.tempImage setAlpha:1.0];
    
    
    UIGraphicsEndImageContext();
    }
    
    
    }
    

    【讨论】:

      【解决方案2】:

      你可以在任何地方调用这个方法。

      - (void)eraseLine
      {
      
          UIGraphicsBeginImageContext(self.bounds.size);
      
          [self.viewImage drawInRect:self.bounds];
      
          CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);
      
          CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
          CGContextSetLineWidth(UIGraphicsGetCurrentContext(), lineWidth);
          CGContextBeginPath(UIGraphicsGetCurrentContext());
          CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);
          CGContextMoveToPoint(UIGraphicsGetCurrentContext(), previousPoint.x, previousPoint.y);
          CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
      
          CGContextStrokePath(UIGraphicsGetCurrentContext());
          self.viewImage = UIGraphicsGetImageFromCurrentImageContext();
          UIGraphicsEndImageContext();
          previousPoint = currentPoint;
      
          [self setNeedsDisplay];
      
      }
      

      【讨论】:

        猜你喜欢
        • 2023-03-16
        • 1970-01-01
        • 1970-01-01
        • 2012-08-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多