我使用以下代码在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();
}
答案 0 :(得分:0)
我想出来了。我在 - (void)draw2的开头放置了“clear2”擦除的if语句{通过在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();
}
}
答案 1 :(得分:0)
你可以在任何地方调用此方法。
- (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];
}