[英]How to draw a line with a custom style using Core Graphics?
我目前正在使用 Core Graphics 绘制一条线。 它真的很简单而且很简单。
- (void)drawRect:(CGRect)rect {
CGContextRef c = UIGraphicsGetCurrentContext();
CGFloat red[4] = {1.0f, 0.0f, 0.0f, 1.0f};
CGContextSetStrokeColor(c, red);
CGContextBeginPath(c);
CGContextMoveToPoint(c, 5.0f, 5.0f);
CGContextAddLineToPoint(c, 300.0f, 600.0f);
CGContextSetLineWidth(c, 25);
CGContextSetLineCap(c, kCGLineCapRound);
CGContextStrokePath(c);
}
这很好用。 假设我们想要绘制一条自定义样式线。 例如,假设我们想模仿蜡笔的风格。 设计师递给你蜡笔风格的图片: http://imgur.com/a/N40ig
要做到这一点,我想我需要做这样的事情:
创建一个特殊的彩色版本的crayonImage1-crayonImage4
每次添加一行到一行时,您都会使用其中一个 crayonImages
每次绘制一个点时,您交替使用蜡笔图像。
第 1 步是有道理的。 我可以使用以下方法:
- (UIImage *)image:(UIImage *)img withColor:(UIColor *)color {
// begin a new image context, to draw our colored image onto
UIGraphicsBeginImageContext(img.size);
// get a reference to that context we created
CGContextRef context = UIGraphicsGetCurrentContext();
// set the fill color
[color setFill];
// translate/flip the graphics context (for transforming from CG* coords to UI* coords
CGContextTranslateCTM(context, 0, img.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
// set the blend mode to color burn, and the original image
CGContextSetBlendMode(context, kCGBlendModeColorBurn);
CGRect rect = CGRectMake(0, 0, img.size.width, img.size.height);
CGContextDrawImage(context, rect, img.CGImage);
// set a mask that matches the shape of the image, then draw (color burn) a colored rectangle
CGContextClipToMask(context, rect, img.CGImage);
CGContextAddRect(context, rect);
CGContextDrawPath(context,kCGPathFill);
// generate a new UIImage from the graphics context we drew onto
UIImage *coloredImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//return the color-burned image
return coloredImg;
}
我不确定如何完成第 2 步和第 3 步。CoreGraphics 中是否有 API 用于将图像设置为线点? 如果是这样,它是什么,我该如何使用它?
提前致谢,
-大卫
从以下示例开始: http://www.ifans.com/forums/showthread.php?t=132024
但是对于画笔,不要画线。 只需使用 CGContextDrawImage 绘制画笔图像。
基本上,您只需为每次触摸绘制一个图像。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.