美文网首页
绘制虚线-ios

绘制虚线-ios

作者: 哎呦我去叫什么呢 | 来源:发表于2019-11-27 15:42 被阅读0次

    初始化一个UIImageView

    初始化一个UIImageView
     UIImageView * imagecontent = [[UIImageView alloc] initWithFrame:CGRectMake(RESIZE(8), RESIZE(118),RESIZE(326), RESIZE(2))];
        [self.backView addSubview:imagecontent];
    //将image设置为drawLineOfDashByImageView返回的Image
    imagecontent.image = [self drawLineOfDashByImageView:imageV];
    /**
     *  通过 Quartz 2D 在 UIImageView 绘制虚线
     *
     *  param imageView 传入要绘制成虚线的imageView
     *  return
     */
    
    - (UIImage *)drawLineOfDashByImageView:(UIImageView *)imageView {
        // 开始划线 划线的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,3};
        
        CGContextSetStrokeColorWithColor(line, [UIColor colorWithRed:0.88 green:0.88 blue:0.88 alpha:1.00].CGColor);
        // 开始绘制虚线
        CGContextSetLineDash(line, 0, lengths, 2);
        
        CGContextMoveToPoint(line, 0.0, 2.0);
        
        CGContextAddLineToPoint(line, RESIZE(326), 2.0);
        
        CGContextStrokePath(line);
        
        // UIGraphicsGetImageFromCurrentImageContext()返回的就是image
        return UIGraphicsGetImageFromCurrentImageContext();
    }
    
    

    相关文章

      网友评论

          本文标题:绘制虚线-ios

          本文链接:https://www.haomeiwen.com/subject/npipwctx.html