简体   繁体   English

如何使用 Core Graphics 绘制具有自定义样式的线?

[英]How to draw a line with a custom style using Core Graphics?

I'm currently drawing a line using Core Graphics.我目前正在使用 Core Graphics 绘制一条线。 It's really bare bones and simple.它真的很简单而且很简单。

- (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);
}

This works well.这很好用。 Let's say that we wanted to draw a custom style line.假设我们想要绘制一条自定义样式线。 Say we wanted to imitate the style of a crayon for example.例如,假设我们想模仿蜡笔的风格。 And that the designer handed your crayon style images: http://imgur.com/a/N40ig设计师递给你蜡笔风格的图片: http://imgur.com/a/N40ig

To do accomplish this effect I think I need to do something like this:要做到这一点,我想我需要做这样的事情:

  1. Create a special colored versions of crayonImage1-crayonImage4创建一个特殊的彩色版本的crayonImage1-crayonImage4

  2. Every time you add a line to line you use one of the crayonImages每次添加一行到一行时,您都会使用其中一个 crayonImages

  3. You alternate the crayonImages every time you draw a point.每次绘制一个点时,您交替使用蜡笔图像。

Step 1 makes sense.第 1 步是有道理的。 I can use the following method:我可以使用以下方法:

- (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;
}

I'm unsure how I can complete steps 2 and 3. Is there an API in CoreGraphics for setting an image as the point of line?我不确定如何完成第 2 步和第 3 步。CoreGraphics 中是否有 API 用于将图像设置为线点? If so what is it and how can I use it?如果是这样,它是什么,我该如何使用它?

Thanks in advance,提前致谢,

-David -大卫

Start with the following example: http://www.ifans.com/forums/showthread.php?t=132024从以下示例开始: http://www.ifans.com/forums/showthread.php?t=132024

But for brushes, don't draw a line.但是对于画笔,不要画线。 Simply draw the brush image using CGContextDrawImage.只需使用 CGContextDrawImage 绘制画笔图像。

Basically, you simply draw an image for every touch.基本上,您只需为每次触摸绘制一个图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM