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設計用於處理位圖,並對其進行高度優化以正確處理它們。畫一幅圖像就像在公園散步:) –