如何删除我在iphone sdk中的图像上绘制的上下文?

时间:2011-04-15 06:58:05

标签: objective-c

我在我的视图上有一个图像,我在图像上绘制颜色,如下面的代码:

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

    mouseSwiped = NO;
    UITouch *touch = [touches anyObject];

    if ([touch tapCount] == 2) 
    {
        //imageDraw.image = nil;
        return;
    }
    else
    {

    }
    lastPoint = [touch locationInView:imageDraw];
    lastPoint.y -= 5;

}


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
    mouseSwiped = YES;

    UITouch *touch = [touches anyObject];   
    CGPoint currentPoint = [touch locationInView:mView];
    currentPoint.y -= 5;


    UIGraphicsBeginImageContext(imageDraw.frame.size);
    [imageDraw.image drawInRect:CGRectMake(0, 0, imageDraw.frame.size.width, imageDraw.frame.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
    //CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 0.5);
    DrawAppDelegate *appDelegate=(DrawAppDelegate*)[ [UIApplication sharedApplication] delegate];
    UIColor *clr = appDelegate.txtColor;
    [clr setStroke];

    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    imageDraw.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    lastPoint = currentPoint;

    mouseMoved++;

    if (mouseMoved == 10) 
    {
        mouseMoved = 0;
    }

}




- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{
    DrawAppDelegate *appDelegate=(DrawAppDelegate*)[ [UIApplication sharedApplication] delegate];
    UITouch *touch = [touches anyObject];

    if ([touch tapCount] == 2)
    {
        //imageDraw.image = nil;
        return;
    }
    else
    {

    }
    if(!mouseSwiped) 
        {
            UIGraphicsBeginImageContext(imageDraw.frame.size);
            [imageDraw.image drawInRect:CGRectMake(0, 0, imageDraw.frame.size.width, imageDraw.frame.size.height)];
            CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
            CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
            UIColor *clr = appDelegate.txtColor;
            [clr setStroke];
            CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
            CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
            CGContextStrokePath(UIGraphicsGetCurrentContext());
            CGContextFlush(UIGraphicsGetCurrentContext());
            imageDraw.image = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
        }
    }


-(void)drawText
{
    /*TextViewController *viewController = [[TextViewController alloc]init];
    [self.navigationController pushViewController:viewController animated:YES];
    [viewController release];
     */

    colorWheel = [[ColorPickerImageView alloc] initWithFrame:CGRectMake(20,20,300,340)]; 
    [colorWheel setImage:[UIImage imageNamed:@"colorWheel1.png"]]; 
    [colorWheel setUserInteractionEnabled:YES];
    colorWheel.backgroundColor = [UIColor clearColor];
    colorWheel.pickedColorDelegate = self;
    [mView addSubview:colorWheel];
    [self animateColorWheelToShow:YES duration:0.3];
}

- (void) pickedColor:(UIColor*)color 
{
    //mView.backgroundColor= color;
    DrawAppDelegate *appDelegate=(DrawAppDelegate*)[ [UIApplication sharedApplication] delegate];
    [self animateColorWheelToShow:NO duration:0.3];
    appDelegate.txtColor = color;
    //[self setNeedsDisplayInRect:CGRectMake(0, 0, 320, 480)]; 
    [mView setNeedsDisplay];
}

现在我要删除图像上的一些彩色颜色。任何人都可以建议我如何使用一些示例代码做任何想法。

任何人的帮助都会深深体会。

感谢所有人, Monish。

2 个答案:

答案 0 :(得分:2)

答案是按行的顺序排列。清除并擦除使用clearColor。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];

    self.previousPoint1 = [touch previousLocationInView:self.imageView];
    self.previousPoint2 = [touch previousLocationInView:self.imageView];
    self.currentPoint = [touch locationInView:self.imageView];
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];

    self.previousPoint2 = self.previousPoint1;
    self.previousPoint1 = [touch previousLocationInView:self.imageView];
    self.currentPoint = [touch locationInView:self.imageView];

    // calculate mid point
    CGPoint mid1 = midPoint(self.previousPoint1, self.previousPoint2);
    CGPoint mid2 = midPoint(self.currentPoint, self.previousPoint1);

    UIGraphicsBeginImageContext(self.imageView.frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [self.imageView.image drawInRect:CGRectMake(0, 0, self.imageView.frame.size.width, self.imageView.frame.size.height)];

    if (self.segmentedControl.selectedSegmentIndex == 11) {
        CGContextSetBlendMode(context, kCGBlendModeClear);
    } else {
        CGContextSetBlendMode(context, kCGBlendModeNormal);
    }

    CGContextMoveToPoint(context, mid1.x, mid1.y);
    // Use QuadCurve is the key
    CGContextAddQuadCurveToPoint(context, self.previousPoint1.x, self.previousPoint1.y, mid2.x, mid2.y);

    CGContextSetLineCap(context, kCGLineCapRound);
    CGContextSetLineWidth(context, 20.0);
    [self.color setStroke];
    CGContextStrokePath(context);

    self.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
}

- (NSArray *)colors {
    return @[[UIColor colorWithRed:249/255.0 green:195/255.0 blue:207/255.0 alpha:1.0],
             [UIColor colorWithRed:225/255.0 green:57 /255.0 blue:156/255.0 alpha:1.0],
             [UIColor colorWithRed:97 /255.0 green:48 /255.0 blue:138/255.0 alpha:1.0],
             [UIColor colorWithRed:255/255.0 green:199/255.0 blue:69 /255.0 alpha:1.0],
             [UIColor colorWithRed:245/255.0 green:125/255.0 blue:55 /255.0 alpha:1.0],
             [UIColor colorWithRed:194/255.0 green:25 /255.0 blue:48 /255.0 alpha:1.0],
             [UIColor colorWithRed:135/255.0 green:214/255.0 blue:242/255.0 alpha:1.0],
             [UIColor colorWithRed:23 /255.0 green:185/255.0 blue:181/255.0 alpha:1.0],
             [UIColor colorWithRed:0  /255.0 green:77 /255.0 blue:119/255.0 alpha:1.0],
             [UIColor colorWithRed:0  /255.0 green:104/255.0 blue:61 /255.0 alpha:1.0],
             [UIColor colorWithRed:0  /255.0 green:163/255.0 blue:89 /255.0 alpha:1.0],
             [UIColor clearColor],
             [UIColor clearColor],
             [UIColor clearColor],
             [UIColor clearColor]
             ];
}

答案 1 :(得分:0)

这样做..

用于选择yor图像的某些部分进行擦除

给出这段代码,

UIColor * selectedColor;

  

self.selectedColor = [UIColor whiteColor];

选择白色的特定部分..

谢谢你..