可直接調(diào)用此方法虎眨,傳入imageView和點(diǎn)數(shù)組。
func drawLineInImageWithImage(imageView:UIImageView,points:[CGPoint]){
UIGraphicsBeginImageContext(imageView.frame.size);
imageView.image?.drawInRect(CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height));
CGContextSetLineCap(UIGraphicsGetCurrentContext(), CGLineCap.Round);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
CGContextSetAllowsAntialiasing(UIGraphicsGetCurrentContext(), true);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), self.lineColor.r(),self.lineColor.g(),self.lineColor.b(),self.lineColor.a());
CGContextBeginPath(UIGraphicsGetCurrentContext());
let begin = points[0];
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), begin.x,begin.y);//起點(diǎn)坐標(biāo)
for point in points{
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), point.x, point.y);
}
//終點(diǎn)坐標(biāo)
CGContextStrokePath(UIGraphicsGetCurrentContext());
imageView.image=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}