画虚线方法


利用ImageView画虚线

#pragma mark--- 画虚线方法 ---   type传递1时,为划横线    type传递非1时,为划竖线

- (UIImage *)drawLineOfDashByImageView:(UIImageView *)imageView AndStyle:(int )type{
    // 开始划线 划线的frame
    UIGraphicsBeginImageContext(imageView.frame.size);
    
    [imageView.image drawInRect:CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height)];
    
    // 获取上下文
    CGContextRef line = UIGraphicsGetCurrentContext();
    
    // 设置线条终点的形状
    CGContextSetLineCap(line, kCGLineCapRound);
    // 设置虚线的长度 和 间距
    CGFloat lengths[] = {3,2};
    
    CGContextSetStrokeColorWithColor(line, MYBlue.CGColor);
    // 开始绘制虚线
    CGContextSetLineDash(line, 0, lengths, 2);
    
    
    if(type==1)
    {
      //表示为横线
        //第一个是线,第二个第三个参数分别是对照这个UIImageView的终点坐标
        CGContextMoveToPoint(line, 0.0, 2);
        
        //   //第一个是线,第二个第三个参数分别是对照这个UIImageView的起点坐标
          CGContextAddLineToPoint(line, imageView.frame.size.width,2 );
        
       
    
    }
    else
    {
        //第一个是线,第二个第三个参数分别是对照这个UIImageView的起点坐标
        CGContextMoveToPoint(line, 2, 0.0);
      
       //第一个是线,第二个第三个参数分别是对照这个UIImageView的终点坐标
        CGContextAddLineToPoint(line,2,imageView.frame.size.height);
    }
    
    CGContextStrokePath(line);
    
    // UIGraphicsGetImageFromCurrentImageContext()返回的就是image
    return UIGraphicsGetImageFromCurrentImageContext();
}

猜你喜欢

转载自blog.csdn.net/guosiyuan1993/article/details/78037296