【发布时间】:2014-10-08 07:25:29
【问题描述】:
我正在为手绘开发一个画笔应用程序,我是核心图形框架的新手。
现在我的应用程序准备好仅使用以下代码进行绘图
override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!)
{
println("Enter in touchesBegan method");
mouseSwiped = false;
var touch : UITouch = touches.anyObject() as UITouch;
lastPoint = touch.locationInView(self.view);
}
override func touchesMoved(touches: NSSet!, withEvent event: UIEvent!)
{
println("Enter in touchesMoved method");
mouseSwiped = true;
var touch : UITouch = touches.anyObject() as UITouch;
var currentPoint : CGPoint = touch.locationInView(self.view);
UIGraphicsBeginImageContext(self.view.frame.size);
self.tempDrawImage!.image?.drawInRect(CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height));
println("lastPoint.X :\(lastPoint!.x) , lastPoint.Y :\(lastPoint!.y)")
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint!.x, lastPoint!.y);
println("currentPoint.X :\(currentPoint.x) , currentPoint.Y :\(currentPoint.y)")
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red!, green!, blue!, 1.0)
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush!);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeNormal);
CGContextStrokePath(UIGraphicsGetCurrentContext());
//contexts!.addObject(UIGraphicsGetCurrentContext());
self.tempDrawImage!.image = UIGraphicsGetImageFromCurrentImageContext();
tempDrawImage!.alpha = opacity!;
UIGraphicsEndImageContext();
println("dot");
lastPoint = currentPoint;
}
override func touchesEnded(touches: NSSet!, withEvent event: UIEvent!)
{
println("Enter in touchesEnded method");
if mouseSwiped == false
{
println("false");
UIGraphicsBeginImageContext(self.view.frame.size);
self.tempDrawImage!.image?.drawInRect(CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height));
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush!);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red!, green!, blue!, opacity!);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint!.x, lastPoint!.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint!.x, lastPoint!.y);
CGContextStrokePath(UIGraphicsGetCurrentContext())
CGContextFlush(UIGraphicsGetCurrentContext());
self.tempDrawImage!.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
UIGraphicsBeginImageContext(self.mainImage!.frame.size);
self.mainImage!.image?.drawInRect(CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height ), blendMode: kCGBlendModeNormal, alpha: 1.0);
self.tempDrawImage!.image.drawInRect(CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height), blendMode: kCGBlendModeNormal, alpha: opacity!);
self.mainImage!.image = UIGraphicsGetImageFromCurrentImageContext();
self.tempDrawImage!.image = nil
UIGraphicsEndImageContext();
}
但在某些应用程序中,我看到它们在其应用程序中提供撤消功能,因此我也想提供此功能,如果使用绘制 2 行并按下撤消按钮,而不是最后添加的行从图像视图中一一删除
我对此进行了搜索,但我无法理解,并且没有一个答案被接受,所以我很困惑,所以请帮助我。
谢谢!
【问题讨论】:
标签: ios core-graphics cgcontext