我可以在图像视图上创建另一个图层以进行绘制

时间:2013-01-18 08:05:40

标签: iphone ios

  

我想在背景图像上绘图,当我将删除该绘图然后仅删除绘制而不是背景图像?我怎样才能完成该任务。通过此代码,我的背景图像也被删除。

   ` UITouch *touch = [touches anyObject];
    previousPoint2 = previousPoint1;
    previousPoint1 = [touch previousLocationInView:self.view];
    currentPoint = [touch locationInView:self.view];

    CGPoint mid1 = midPoint(previousPoint1, previousPoint2);
    CGPoint mid2 = midPoint(currentPoint, previousPoint1);

    UIGraphicsBeginImageContext(imageDoodle.frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();


    [imageDoodle.image drawInRect:CGRectMake(0, 0, imageDoodle.frame.size.width, imageDoodle.frame.size.height)];


    CGContextMoveToPoint(context, mid1.x, mid1.y);


    CGContextAddQuadCurveToPoint(context, previousPoint1.x, previousPoint1.y, mid2.x, mid2.y);

    CGContextSetLineCap(context, kCGLineCapRound);

     if(IsErase)
    {
    CGContextSetBlendMode(context,kCGBlendModeClear);
    }

    else
    {
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), redvalue, greenvalue, bluevalue, 1.0);
    }

    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 8.0);


    CGContextStrokePath(UIGraphicsGetCurrentContext());

     imageDoodle.image = UIGraphicsGetImageFromCurrentImageContext();
     UIGraphicsEndImageContext();

}

`

2 个答案:

答案 0 :(得分:0)

another UIImageView添加到您的imageDoodle

//Add outer ImgView for Drawing purpose
imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0,imageDoodle.frame.size.width, imageDoodle.frame.size.height)];
imgView.userInteractionEnabled  = YES;
imgView.multipleTouchEnabled = YES;
[imageDoodle addSubview:imgView];

编辑:现在使用相同的代码,但imgView使用drawing purpose

[imgView.image drawInRect:CGRectMake(0, 0, imageDoodle.frame.size.width, imageDoodle.frame.size.height)];

imgView

中设置当前图像
imgView.image = UIGraphicsGetImageFromCurrentImageContext();

现在将original image设为主ImageView

现在render主要ImageView获取backgroundimage+drawing

答案 1 :(得分:0)

将透明UIView添加到ImageView并将其高度和宽度与imageView或imagesize成比例。无论你想绘制什么,用uiview绘制并擦除它。