1
我目前正在使用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
每当你添加一条线,你使用蜡笔画之一
你每次绘制一个点时交替蜡笔画。
步骤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?如果是这样,我该如何使用它?
由于提前,
-David
每次都不画刷图像变得很慢? – bobbypage
您不会每次都绘制整个历史。检查代码:绘制内容后,用新图像(包含新笔刷)替换图像内容。下一个抽签将会在前一个抽签之上。只要你不关心历史(撤销/重做),你不需要担心表演。请记住 - QuartzCore设计用于处理位图,并对其进行高度优化以正确处理它们。画一幅图像就像在公园散步:) –