2013-07-19 190 views
0

我是Iphone開發新手。邊緣填充顏色

目前我正在着色應用程序。

我使用蘋果的繪畫應用程序作爲參考創建我的應用程序。

我成功地創建應用程序,其中u可在給定的紋理圖像的畫面顏色

我做什麼是我 創建延伸OpenGL的一個自定義的UIView,我發現它的觸摸,並相應地繪製。 我還保留了包含大綱圖像的背景UIImageView,所以感覺就像您在上面的圖像那樣。

一切正常 但我想填充顏色黑邊

一樣,如果一個圖像有四方形它有黑邊和方形的內部是空的,如果我觸摸任何方應該填補這一方與選擇的顏色(主要是我工作的不規則形狀)

誰能告訴我怎樣才能填補這一方

洪水填充算法中看起來慢,因爲我有這將需要時間來填寫一些大的圖像中的顏色顏色

所以有沒有簡單的方法,通過它我可以填充顏色

一個示例代碼將b非常有幫助的,因爲我是新來的iPhone開發

回答

1

我implemnted這種特徵在我最近的項目。不同之處在於:我只在邊框填充顏色。

檢查我的代碼在這裏,它可能會幫助你

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

欣賞節目!

+0

在我給我所有的四方形單個圖像 裏面,所以我需要檢測每平方米的如(還記得我說過我工作的不規則形狀) 那麼,如何檢測這些部分 我是否需要使用CGpath繪製這些形狀? – chavo218

+0

是的,這也會檢測不規則形狀的邊界。這絕對不是爲你填充顏色內部的形狀,我重複這是爲着色只有邊框/框架。但你可以通過這個想法來實現你的願望。只需要經過一次 –

+0

我正在使用opengl。我沒有使用Core Graphics – chavo218