[英]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.