绘制虚线-ios

2019-11-27  本文已影响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();
}

上一篇下一篇

猜你喜欢

热点阅读