如何使用CGIOS绘制虚线?

如何使用CGIOS绘制虚线?,ios,objective-c,cgcontext,Ios,Objective C,Cgcontext,我已经用cgpoints绘制了路径,但是路径上布满了虚线。我想画虚线 我的代码: CGPoint Point1=CGPointMake(184.9489, 383.4082); CGPoint Point2=CGPointMake(184.9489, 383.4082); CGPoint Point3=CGPointMake(184.2969, 377.911); CGPoint Point4=CGPointMake(184.2969, 377.911); CGPoint Point5=CGPo

我已经用cgpoints绘制了路径,但是路径上布满了虚线。我想画虚线

我的代码:

CGPoint Point1=CGPointMake(184.9489, 383.4082);
CGPoint Point2=CGPointMake(184.9489, 383.4082);
CGPoint Point3=CGPointMake(184.2969, 377.911);
CGPoint Point4=CGPointMake(184.2969, 377.911);
CGPoint Point5=CGPointMake(184.2443, 372.978);
CGPoint Point6=CGPointMake(184.2443, 372.978);
NSArray *arry1=[NSArray arrayWithObjects:[NSNumber numberWithFloat:Point1.x],[NSNumber numberWithFloat:Point3.x],[NSNumber numberWithFloat:Point5.x],nil];
NSArray *arry2=[NSArray arrayWithObjects:[NSNumber numberWithFloat:Point1.y],[NSNumber numberWithFloat:Point3.y],[NSNumber numberWithFloat:Point5.y],nil];
NSArray *arry3=[NSArray arrayWithObjects:[NSNumber numberWithFloat:Point2.x],[NSNumber numberWithFloat:Point4.x],[NSNumber numberWithFloat:Point6.x],nil];
NSArray *arry4=[NSArray arrayWithObjects:[NSNumber numberWithFloat:Point2.y],[NSNumber numberWithFloat:Point4.y],[NSNumber numberWithFloat:Point6.y],nil];
for(int i=0;i<[arry2 count];i++)
{
    UIGraphicsBeginImageContext(self.view.frame.size);
    [img_View.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 3 );
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(),
                                     [UIColor blueColor].CGColor);
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), [[arry1 objectAtIndex:i]floatValue], [[arry2 objectAtIndex:i]floatValue]);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), [[arry3 objectAtIndex:i]floatValue], [[arry4 objectAtIndex:i]floatValue]);
    CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeColor);
    CGContextDrawPath(UIGraphicsGetCurrentContext(), kCGPathFillStroke);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    img_View.image = UIGraphicsGetImageFromCurrentImageContext();
    [img_View setAlpha:1];
    UIGraphicsEndImageContext();}
CGPoint Point1=CGPointMake(184.9489383.4082);
CGPoint Point2=CGPointMake(184.9489383.4082);
CGPoint Point3=CGPointMake(184.2969377.911);
CGPoint Point4=CGPointMake(184.2969377.911);
CGPoint Point5=CGPointMake(184.2443372.978);
CGPoint Point6=CGPointMake(184.2443372.978);
NSArray*arry1=[NSArray arrayWithObjects:[NSNumber numberWithFloat:Point1.x],[NSNumber numberWithFloat:Point3.x],[NSNumber numberWithFloat:Point5.x],无];
NSArray*arry2=[NSArray arrayWithObjects:[NSNumber numberWithFloat:Point1.y]、[NSNumber numberWithFloat:Point3.y]、[NSNumber numberWithFloat:Point5.y]、nil];
NSArray*arry3=[NSArray arrayWithObjects:[NSNumber numberWithFloat:Point2.x],[NSNumber numberWithFloat:Point4.x],[NSNumber numberWithFloat:Point6.x],无];
NSArray*arry4=[NSArray arrayWithObjects:[NSNumber numberWithFloat:Point2.y]、[NSNumber numberWithFloat:Point4.y]、[NSNumber numberWithFloat:Point6.y]、nil];

对于(int i=0;i而言,问题在于每个循环中,直线的起点和终点是相同的点,因此它以点的形式绘制。如果记录点值,您将看到它。以下代码将绘制两条直线

UIGraphicsBeginImageContext(self.view.frame.size);
[_imageView.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 3.0);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetStrokeColorWithColor(context,
                                 [UIColor greenColor].CGColor);

for(int i=0;i<arry2.count;i++)
{
    if (i == 0)
    CGContextMoveToPoint(context,[[arry1 objectAtIndex:i]floatValue] ,[[arry2 objectAtIndex:i]floatValue]);
    CGContextAddLineToPoint(context, [[arry3 objectAtIndex:i]floatValue], [[arry4 objectAtIndex:i]floatValue]);
}
CGContextSetBlendMode(context,kCGBlendModeColor);
CGContextDrawPath(context, kCGPathFillStroke);
CGContextStrokePath(context);

_imageView.image = UIGraphicsGetImageFromCurrentImageContext();
[_imageView setAlpha:1];
UIGraphicsEndImageContext();
UIGraphicsBeginImageContext(self.view.frame.size);
[\u imageView.image drawInRect:CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height)];
CGContextRef context=UIGraphicsGetCurrentContext();
CGContextSetLineWidth(上下文,3.0);
CGContextSetLineCap(上下文,kCGLineCapRound);
CGContextSetStrokeColorWithColor(上下文,
[UIColor greenColor].CGColor);
对于(int i=0;i