橡皮擦不工作在iOS绘图 [英] eraser not working in iOS drawing

查看:260
本文介绍了橡皮擦不工作在iOS绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个绘图项目,其中我有一个橡皮擦选项。下面给出的代码用于当我启动我的应用程序,绘制一些行,并继续使用橡皮擦。它工作正常,我得到橡皮擦效果。现在第二种情况是我绘制了10行,然后点击撤消按钮并撤消整个事情,然后我重做整个事情,现在当我点击橡皮擦按钮,并尝试擦除一些部分,而是清除整个图形。这是我想要弄清楚的,但我不知道我错了什么,所以朋友,请帮助我。



下面是我的代码。 / p>

   - (void)drawRect:(CGRect)rect 
{


case DRAW:
{
[m_curImage drawAtPoint:CGPointMake(0,0)];

CGPoint mid1 = midPoint(m_previousPoint1,m_previousPoint2);
CGPoint mid2 = midPoint(m_currentPoint,m_previousPoint1);
CGContextRef context = UIGraphicsGetCurrentContext();



[self.layer renderInContext:context];
CGContextMoveToPoint(context,mid1.x,mid1.y);
CGContextAddQuadCurveToPoint(context,m_previousPoint1.x,m_previousPoint1.y,mid2.x,mid2.y);
CGContextSetLineCap(context,kCGLineCapRound);
CGContextSetLineWidth(context,self.lineWidth);
CGContextSetStrokeColorWithColor(context,self.lineColor.CGColor);
CGContextSetAlpha(context,self.lineAlpha);
CGContextSetAllowsAntialiasing(context,YES);
CGContextStrokePath(context);
// [super drawRect:rect];
}
break;



case ERASE:
{
[m_curImage drawAtPoint:CGPointMake(0,0)];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClearRect(context,rect);
CGContextSetBlendMode(context,kCGBlendModeClear);
[super drawRect:rect];
break;

}


case UNDO:
{
[m_curImage drawInRect:self.bounds];
break;

}

case REDO:
{
[m_curImage drawInRect:self.bounds];
break;
}


默认值:
break;
}
}

这些是当我点击undo /重做。

   - (void)redrawLine 
{
UIGraphicsBeginImageContextWithOptions(self.bounds.size, 0.0f);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
NSDictionary * lineInfo = [m_lineArray lastObject];
m_curImage =(UIImage *)[li​​neInfo valueForKey:@IMAGE];
UIGraphicsEndImageContext();
[self setNeedsDisplayInRect:self.bounds];
}


- (void)undoButtonClicked
{
if([m_lineArray count]> 0)
{
NSMutableArray * line = [m_lineArray lastObject];
[m_bufferArray addObject:line];
[m_lineArray removeLastObject];
[self redrawLine];
}
m_drawStep = UNDO;

}

- (void)redoButtonClicked
{
if([m_bufferArray count]> 0)
{
NSMutableArray * line = [m_bufferArray lastObject];
[m_lineArray addObject:line];
[m_bufferArray removeLastObject];
[self redrawLine];
}
m_drawStep = REDO;

}

请告诉我是否正确。 p>

尊敬的,



Ranjit

解决方案

我已经解决了这个问题,下面的问题和代码工作完美

  case:ERASE 
{
CGPoint mid1 = midPoint(m_previousPoint1,m_previousPoint2);
CGPoint mid2 = midPoint(m_currentPoint,m_previousPoint1);

[m_curImage drawAtPoint:CGPointMake(0,0)];
CGContextRef context = UIGraphicsGetCurrentContext();

[self.layer renderInContext:context];
CGContextMoveToPoint(context,mid1.x,mid1.y);
CGContextAddQuadCurveToPoint(context,m_previousPoint1.x,m_previousPoint1.y,mid2.x,mid2.y);
CGContextSetLineCap(context,kCGLineCapRound);


CGContextSetBlendMode(context,kCGBlendModeClear);
CGContextSetLineJoin(context,kCGLineJoinRound);
CGContextSetLineWidth(context,self.lineWidth);
CGContextSetStrokeColorWithColor(context,self.lineColor.CGColor);
CGContextSetShouldAntialias(context,YES);
CGContextSetAllowsAntialiasing(context,YES);
CGContextSetFlatness(context,0.1f);

CGContextSetAlpha(context,self.lineAlpha);
CGContextStrokePath(context);
}

尊敬
Ranjit。



这解决了我的问题,如果任何人有相同的问题,请实施此。


I am working on a drawing project, where I have an eraser option. The code given below is for when I start my app and draw some lines and the proceed to use the eraser. It works fine and I get the eraser effect. Now the second scenario is where I draw some 10 lines and then click on the "undo button" and undo the whole thing, then I redo the whole thing, and now when I click on the "eraser button" and try to erase some part, but instead, it will clear the whole drawing. This is what I am trying to figure out, but i'm not understanding where I am going wrong so friends, please help me out.

Below is my code.

- (void)drawRect:(CGRect)rect
{


      case DRAW:
    {
        [m_curImage drawAtPoint:CGPointMake(0, 0)];

        CGPoint mid1 = midPoint(m_previousPoint1, m_previousPoint2); 
        CGPoint mid2 = midPoint(m_currentPoint, m_previousPoint1);
        CGContextRef context = UIGraphicsGetCurrentContext(); 



        [self.layer renderInContext:context];
        CGContextMoveToPoint(context, mid1.x, mid1.y);
        CGContextAddQuadCurveToPoint(context, m_previousPoint1.x, m_previousPoint1.y, mid2.x, mid2.y); 
        CGContextSetLineCap(context, kCGLineCapRound);
        CGContextSetLineWidth(context, self.lineWidth);
        CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
        CGContextSetAlpha(context, self.lineAlpha);
        CGContextSetAllowsAntialiasing(context, YES);
        CGContextStrokePath(context); 
       //            [super drawRect:rect];           
    }
         break;



        case ERASE:
        {
            [m_curImage drawAtPoint:CGPointMake(0, 0)];
            CGContextRef context = UIGraphicsGetCurrentContext();     
            CGContextClearRect(context, rect);                
            CGContextSetBlendMode(context, kCGBlendModeClear);
            [super drawRect:rect];
            break;                

  }      


        case UNDO:
        {
            [m_curImage drawInRect:self.bounds];
             break;

        }

        case REDO:
        {
            [m_curImage drawInRect:self.bounds];  
             break;           
        }


        default:
            break;
    }    
}

These are functions that run when I click on undo/redo.

-(void)redrawLine
{
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0f);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    NSDictionary *lineInfo = [m_lineArray lastObject];
    m_curImage = (UIImage*)[lineInfo valueForKey:@"IMAGE"];
    UIGraphicsEndImageContext();
    [self setNeedsDisplayInRect:self.bounds];   
}


-(void)undoButtonClicked
{
    if([m_lineArray count] > 0)
    {
        NSMutableArray *line = [m_lineArray lastObject];
        [m_bufferArray addObject:line];
        [m_lineArray removeLastObject];
        [self redrawLine];
    }
    m_drawStep = UNDO;

}

-(void)redoButtonClicked
{
    if([m_bufferArray count] > 0)
    {
        NSMutableArray *line = [m_bufferArray lastObject];
        [m_lineArray addObject:line];
        [m_bufferArray removeLastObject];        
        [self redrawLine];
    }
     m_drawStep = REDO;

}

Please tell me whether I am doing it right.

Regards,

Ranjit

解决方案

I have solved this issue, the problem and below is the code which works perfectly

case :ERASE
{
 CGPoint mid1 = midPoint(m_previousPoint1, m_previousPoint2); 
            CGPoint mid2 = midPoint(m_currentPoint, m_previousPoint1);

            [m_curImage drawAtPoint:CGPointMake(0, 0)];
            CGContextRef context = UIGraphicsGetCurrentContext(); 

            [self.layer renderInContext:context];
            CGContextMoveToPoint(context, mid1.x, mid1.y);
            CGContextAddQuadCurveToPoint(context, m_previousPoint1.x, m_previousPoint1.y, mid2.x, mid2.y); 
            CGContextSetLineCap(context, kCGLineCapRound);


            CGContextSetBlendMode(context, kCGBlendModeClear);
            CGContextSetLineJoin(context, kCGLineJoinRound);
            CGContextSetLineWidth(context, self.lineWidth);
            CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
            CGContextSetShouldAntialias(context, YES);  
            CGContextSetAllowsAntialiasing(context, YES); 
            CGContextSetFlatness(context, 0.1f);

            CGContextSetAlpha(context, self.lineAlpha);
            CGContextStrokePath(context); 
}

Regards Ranjit.

This solved my issue, if anyone has the same issue, please implement this.

这篇关于橡皮擦不工作在iOS绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆