[英]Filling Colors inside edges
我是Iphone開發的新手。
目前,我正在制作着色應用程序。
我正在使用蘋果的繪畫應用程序作為創建我的應用程序的參考。
我成功創建了可以在給定紋理圖像的屏幕上着色的應用程序
我所做的是創建一個自定義UIView,該UIView擴展了opengl,並檢測到觸摸並相應繪制。 我還保留了包含輪廓圖像的背景UIImageView,因此感覺就像您在該圖像上方繪制的圖像。
一切正常,但我想在黑色邊緣內填充顏色
就像如果一個圖像有四個具有黑色邊緣的正方形並且該正方形的內部為空白,並且如果我觸摸任何正方形,它都應使用選定的顏色填充該正方形(大多數情況下,我使用的是不規則形狀)
誰能告訴我如何在正方形內填充顏色
洪水填充算法看起來很慢,因為我有一些大圖像需要花一些時間才能填充顏色
所以有什么簡單的方法可以填充顏色
示例代碼將非常有用,因為我是iPhone Dev的新手。
我在最近的項目中實現了這種功能。 區別是:我僅在邊框中填充顏色。
在這里檢查我的代碼,它可能對您有幫助
// apply color to only border & return an image
+ (UIImage *)imageNamed:(NSString *)name withColor:(UIColor *)color
{
// load the image
UIImage *img = [UIImage imageNamed:name];
// 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;
}
享受編程!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.