画虚线 发表于 2017-07-20 | 阅读次数 通过UIImage的绘图方法来绘制绘制一条水平的虚线 123456789101112131415161718192021222324252627282930- (UIImage *)drawDashLineWithSize:(CGSize)size{ UIGraphicsBeginImageContext(size); CGContextRef context = UIGraphicsGetCurrentContext(); //设置线条终点形状 CGContextSetLineCap(context, kCGLineCapRound); // 设置颜色 CGContextSetStrokeColorWithColor(context, [UIColor colorWithWhite:0.408 alpha:1.000].CGColor); CGFloat lengths[] = {10,2}; /* CGContextSetLineDash 此函数需要四个参数: context – 这个不用多说 phase - 表示在第一个虚线绘制的时候跳过多少个点 lengths – lengths的值{10,2}表示先绘制10个点,再跳过2个点 如果把lengths值改为{10, 20, 10},则表示先绘制10个点,跳过20个点,绘制10个点,跳过10个点,再绘制20个点,如此反复 count – lengths数组的长度 */ CGContextSetLineDash(context, 0, lengths, 2); //画虚线 CGContextMoveToPoint(context, 0.0, 2.0); //开始画线 CGContextAddLineToPoint(context, size.width, 2.0); CGContextStrokePath(context); // UIGraphicsGetImageFromCurrentImageContext()返回的就是image UIImage * image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; }